diff --git a/docs/docs/03-syntax-and-usage/12-script-templates.md b/docs/docs/03-syntax-and-usage/12-script-templates.md index 63f4ca234..b37fcc78a 100644 --- a/docs/docs/03-syntax-and-usage/12-script-templates.md +++ b/docs/docs/03-syntax-and-usage/12-script-templates.md @@ -188,6 +188,42 @@ The data in the script tag can then be accessed from client-side JavaScript. const data = JSON.parse(document.getElementById('id').textContent); ``` +Alternatively, you can add a helper that defines a constant, making it accessible in JavaScript scripts as a `const` variable. + +```templ +// helper/helper.go +package helper + +import ( + "fmt" + "github.com/a-h/templ" +) + +func AddScriptVar(k string, v string) templ.Component { + raw_script := fmt.Sprintf(``, k, v) + return templ.Raw(raw_script) +} + + +// layout/base.templ +import "helper" + +const HEADER = "xyztt" +templ Base() { + + ... + + ... + @helper.AddScriptVar("HX_header", HEADER) + + + +} +``` + ## Working with NPM projects https://github.com/a-h/templ/tree/main/examples/typescript contains a TypeScript example that uses `esbuild` to transpile TypeScript into plain JavaScript, along with any required `npm` modules.