[Django] #35182: Add support for additional HTML attributes to json_script template tag

9 views
Skip to first unread message

Django

unread,
Feb 11, 2024, 4:49:52 PMFeb 11
to django-...@googlegroups.com
#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.

Django

unread,
Feb 11, 2024, 4:50:24 PMFeb 11
to django-...@googlegroups.com
#35182: Add support for additional HTML attributes to json_script template tag
-----------------------------------+--------------------------------------
Reporter: LB (Ben Johnston) | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.0
Severity: Normal | Resolution:
Keywords: template_tag | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Comment (by LB (Ben Johnston)):

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>

Django

unread,
Feb 12, 2024, 1:20:25 PMFeb 12
to django-...@googlegroups.com
#35182: Add support for additional HTML attributes to json_script template tag
-----------------------------------+--------------------------------------
Reporter: LB (Ben Johnston) | Owner: nobody
Type: New feature | Status: closed

Component: Template system | Version: 5.0
Severity: Normal | Resolution: wontfix
Keywords: json_script | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* 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>

Django

unread,
Feb 12, 2024, 2:56:33 PMFeb 12
to django-...@googlegroups.com
#35182: Add support for additional HTML attributes to json_script template tag
-----------------------------------+--------------------------------------
Reporter: LB (Ben Johnston) | Owner: nobody
Type: New feature | Status: closed
Component: Template system | Version: 5.0
Severity: Normal | Resolution: wontfix
Keywords: json_script | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Comment (by LB (Ben Johnston)):

No problems. Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/35182#comment:3>

Reply all
Reply to author
Forward
0 new messages