#35182: Add support for additional HTML attributes to json_script template tag -------------------------------------+------------------------------------- Reporter: LB (Ben | Owner: nobody Johnston) | Type: New | Status: new feature | Component: Template | Version: 5.0 system | Severity: Normal | Keywords: template_tag Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- 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 - 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. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018d9a25d6d3-c0503dca-7eb0-4c89-8fbc-3981622d6daf-000000%40eu-central-1.amazonses.com.