#35182 closed New feature (wontfix)
Add support for additional HTML attributes to json_script template tag
Reported by: | LB (Ben Johnston) | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | 5.0 |
Severity: | Normal | Keywords: | json_script |
Cc: | LB (Ben Johnston) | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The json_script
template filter is useful for bootstrapping server side data into some client-side widgets or JS driven config.
However, in it's current state only a HTML id attribute is supported which means that if you want to leverage low-JS libraries such as Stimulus, HTMX or Alpine.js to 'listen' for the element you have to add an extra layer for finding elements by an id.
Additionally, it makes it difficult to modify other HTML attributes such as nonce
.
While the type being JSON has an intentionally smaller use case than external scripts it would still be very useful to be able to add other attributes.
Example usage
{{ value|json_script:"hello-data"@"data-controller='my-controller'" }}
Would output
<script id="hello-data" type="application/json" data-controller='my-controller'>{"hello": "world"}</script>
I'm not sure on the best separator for the attributes or if a string or some other value would be better. It would also need to support non-id usage.
Links
Change History (3)
comment:1 by , 9 months ago
comment:2 by , 9 months ago
Keywords: | json_script added; template_tag removed |
---|---|
Resolution: | → wontfix |
Status: | new → closed |
Hello Ben, thank you for your report.
Django aims at providing core functionality to the most common use cases, and in my opinion, this is a very concrete/niche use case that your project could easily implement with a custom template filter derived from the json_script
.
I'll be closing this ticket as wontfix
following the ticket triaging process, but if you disagree with the resolution, you can start a new conversation in the Django Forum, where you'll reach a wider audience and likely get extra feedback.
I'm also happy to try to do a PR if this is something others would like to see.