Skip to content

Commit a7dd930

Browse files
Copilota-h
andauthored
docs: use the built-in JSON attribute helper (#1451)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: a-h <1029947+a-h@users.noreply.github.com>
1 parent 97eedaa commit a7dd930

1 file changed

Lines changed: 3 additions & 11 deletions

File tree

‎docs/docs/03-syntax-and-usage/03-attributes.md‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,10 @@ CSS handling is discussed in detail in [CSS style management](/syntax-and-usage/
263263

264264
## JSON attributes
265265

266-
To set an attribute's value to a JSON string (e.g. for htmx's [hx-vals](https://htmx.org/attributes/hx-vals) or Alpine's [x-data](https://alpinejs.dev/directives/data)), serialize the value to a string using a function.
267-
268-
```go
269-
func countriesJSON() string {
270-
countries := []string{"Czech Republic", "Slovakia", "United Kingdom", "Germany", "Austria", "Slovenia"}
271-
bytes, _ := json.Marshal(countries)
272-
return string(bytes)
273-
}
274-
```
266+
To set an attribute's value to a JSON string (e.g. for htmx's [hx-vals](https://htmx.org/attributes/hx-vals) or Alpine's [x-data](https://alpinejs.dev/directives/data)), use `templ.JSONString` to serialize the value.
275267

276268
```templ
277-
templ SearchBox() {
278-
<search-webcomponent suggestions={ countriesJSON() } />
269+
templ SearchBox(countries []string) {
270+
<search-webcomponent suggestions={ templ.JSONString(countries) } />
279271
}
280272
```

0 commit comments

Comments
 (0)