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
- https://docs.djangoproject.com/en/5.0/ref/templates/builtins/#std-
templatefilter-json_script
--
Ticket URL: <https://code.djangoproject.com/ticket/35182>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
I'm also happy to try to do a PR if this is something others would like to
see.
--
Ticket URL: <https://code.djangoproject.com/ticket/35182#comment:1>
* keywords: template_tag => json_script
* status: new => closed
* resolution: => wontfix
Comment:
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
[https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#closing-tickets ticket triaging process], but if you disagree
with the resolution, you can start a new conversation in the
[https://forum.djangoproject.com/c/internals/5 Django Forum], where you'll
reach a wider audience and likely get extra feedback.
--
Ticket URL: <https://code.djangoproject.com/ticket/35182#comment:2>
No problems. Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/35182#comment:3>