You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To pass data from the server to client-side scripts, see [Passing server-side data to scripts](#passing-server-side-data-to-scripts).
18
+
:::tip
19
+
To ensure that a `<script>` tag within a templ component is only rendered once per HTTP response (or context), use a [templ.OnceHandle](18-render-once.md).
19
20
20
-
## Adding client side behaviours to components
21
+
Using a `templ.OnceHandle` allows a component to define global client-side scripts that it needs to run without including the scripts multiple times in the response.
22
+
:::
21
23
22
-
To ensure that a `<script>` tag within a templ component is only rendered once per HTTP response, use a [templ.OnceHandle](18-render-once.md).
24
+
## Pass Go data to JavaScript
23
25
24
-
Using a `templ.OnceHandle` allows a component to define global client-side scripts that it needs to run without including the scripts multiple times in the response.
26
+
### Pass Go data to a JavaScript event handler
27
+
28
+
Use `templ.JSFuncCall` to pass server-side data to client-side scripts by calling a JavaScript function.
The data passed to the `alert` function is JSON encoded, so if `data.Message` was the string value of `Hello, from the JSFuncCall data`, the output would be:
37
+
38
+
```html title="output.html"
39
+
<buttononclick="alert('Hello, from the JSFuncCall data')">Show alert</button>
40
+
```
41
+
42
+
### Pass event objects to an Event Handler
43
+
44
+
HTML element `on*` attributes pass an event object to the function. To pass the event object to a function, use `templ.JSExpression`.
45
+
46
+
47
+
:::warning
48
+
`templ.JSExpression` bypasses JSON encoding, so the string value is output directly to the HTML - this can be a security risk if the data is not trusted, e.g. the data is user input, not a compile-time constant.
49
+
:::
50
+
51
+
```templ title="input.templ"
52
+
<script type="text/javascript">
53
+
function clickHandler(event, message) {
54
+
alert(message);
55
+
event.preventDefault();
56
+
}
57
+
</script>
58
+
<button onclick={ templ.JSFuncCall("clickHandler", templ.JSExpression("event"), "message from Go") }>Show event</button>
59
+
```
60
+
61
+
The output would be:
62
+
63
+
```html title="output.html"
64
+
<scripttype="text/javascript">
65
+
functionclickHandler(event, message) {
66
+
alert(message);
67
+
event.preventDefault();
68
+
}
69
+
</script>
70
+
<buttononclick="clickHandler(event, 'message from Go')">Show event</button>
71
+
```
72
+
73
+
### Call client side functions with server side data
74
+
75
+
Use `templ.JSFuncCall` to call a client-side function with server-side data.
76
+
77
+
`templ.JSFuncCall` takes a function name and a variadic list of arguments. The arguments are JSON encoded and passed to the function.
78
+
79
+
In the case that the function name is invalid (e.g. contains `</script>` or is a JavaScript expression, not a function name), the function name will be sanitized to `__templ_invalid_function_name`.
This will output a `<script>` tag that calls the `functionToCall` function with the `Name` and `Age` properties of the `data` object.
88
+
89
+
```html title="output.html"
90
+
<scripttype="text/javascript">
91
+
functionToCall("John", 42);
92
+
</script>
93
+
```
94
+
95
+
:::tip
96
+
If you want to write out an arbitrary string containing JavaScript, and are sure it is safe, you can use `templ.JSUnsafeFuncCall` to bypass script sanitization.
97
+
98
+
Whatever string you pass to `templ.JSUnsafeFuncCall` will be output directly to the HTML, so be sure to validate the input.
99
+
:::
100
+
101
+
### Pass server-side data to the client in a HTML attribute
102
+
103
+
A common approach used by libraries like alpine.js is to pass data to the client in a HTML attribute.
104
+
105
+
To pass server-side data to the client in a HTML attribute, use `templ.JSONString` to encode the data as a JSON string.
According to Mozilla, [inline event handlers are considered bad practice](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Events#inline_event_handlers_%E2%80%94_dont_use_these).
157
+
158
+
This example demonstrates how to add client-side behaviour to a component using a script tag.
159
+
160
+
The example uses a `templ.OnceHandle` to define global client-side scripts that are required, without rendering the scripts multiple times in the response.
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.
@@ -272,7 +365,9 @@ func main() {
272
365
## Script templates
273
366
274
367
:::warning
275
-
Script templates are a legacy feature and are not recommended for new projects. Use standard `<script>` tags to import a standalone JavaScript file, optionally created by a bundler like `esbuild`.
368
+
Script templates are a legacy feature and are not recommended for new projects.
369
+
370
+
Use the `templ.JSFuncCall`, `templ.JSONString` and other features of templ alongside standard `<script>` tags to import standalone JavaScript files, optionally created by a bundler like `esbuild`.
276
371
:::
277
372
278
373
If you need to pass Go data to scripts, you can use a script template.
0 commit comments