Opened 4 months ago

Last modified 8 days ago

#35535 assigned New feature

Provide new template tag to capture internal block output as a template variable ("simple block tag").

Reported by: Jake Howard Owned by: Jake Howard
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Carlton Gibson, Sébastien Corbin Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django's template system allows creating custom tags, but they don't allow taking content:

{% mysimpletag foo="bar" %}

However there are plenty of use-cases where it'd be useful to also collect content along with the tag, turning it into a "block" tag:

{% mysimpletag foo="bar" %}
  This content gets captured too
{% endmysimpletag %}

This currently requires using complex internals of the template system (some documented, some not) to achieve. The cache tag is a good example of this kind of "simple" block, which requires some parser internals.

I propose adding a @register.simple_block method to allow easy registering of custom block tags. The API would be similar to simple_tag, however with a required content argument containing the (rendered) template content:

@register.simple_block(takes_context=True)
def mysimpletag(context: Context, content: str, foo: str) -> str:
    return f"foo = {foo}"

(Types added for reference)

I have a working demo of this I can push up.

Change History (21)

comment:1 by Sarah Boyce, 4 months ago

Resolution: wontfix
Status: assignedclosed

Hi Jake👋 thank you for raising this, I can see why this could be useful.

For a feature request to be accepted, we want to see some discussion on the forum first to confirm/validate the feature with the community and get some feedback.
I'll close the ticket for now, but if there is a community agreement for this, you are welcome to come back linking the forum topic, so we can then re-open it.
For more details, please see the documented guidelines for requesting features 👍

comment:2 by Jake Howard, 4 months ago

For now, I've now raised this on the forum: https://forum.djangoproject.com/t/feature-proposal-simple-block-tag/32229. Will collect feedback, and then return!

comment:3 by Carlton Gibson, 4 months ago

Cc: Carlton Gibson added

comment:4 by Jake Howard, 3 months ago

Has patch: set
Resolution: wontfix
Status: closednew

Following a few conversations on the forum thread, I've opened a [PR https://github.com/django/django/pull/18343].

comment:5 by Natalia Bidart, 3 months ago

Triage Stage: UnreviewedAccepted
Version: 5.0dev

Thank you Jake for following up on this. I think the forum post shows enough agreement, and I personally also think this is a useful adding. I also agree with related comments from #6378, specifically:

I do think there are cases where the "capture" model is the appropriate one, and it ought to be built-in.

I considered reopening #6378 and closing this as a dupe, but I decided not to because the older ticket is a bit confusing in how the problem is described because there is a mix of requirements which some were solved by #18651.

comment:6 by Natalia Bidart, 3 months ago

Summary: Simple block tagProvide new template tag to capture internal block output as a template variable ("simple block tag").

comment:7 by Sushmita Palikhe, 3 months ago

Owner: changed from Jake Howard to Sushmita Palikhe
Status: newassigned

comment:8 by Natalia Bidart, 3 months ago

Owner: changed from Sushmita Palikhe to Jake Howard

Hello Sushmita Palikhe! Thank you for your interest in contributing to Django, but this ticket is already owned by Jake and their work is still active.

comment:10 by Jake Howard, 3 months ago

Owner: set to Jake Howard

Yes, definitely still active. The draft PR is linked above though, and I'd definitely welcome any comments / suggestions you may have.

comment:11 by Sébastien Corbin, 2 months ago

Cc: Sébastien Corbin added

comment:12 by Sarah Boyce, 2 months ago

Note that #6378 was recently closed as a duplicate to this ticket.

comment:13 by Jake Howard, 2 months ago

I do think there are cases where the "capture" model is the appropriate one, and it ought to be built-in.

Currently, the simple block is added with a related implementation. It should be simple enough to use it to implement a capture tag, but I wasn't going to add it to the existing PR to avoid scope-creep. I can if you think it'd be better?

comment:14 by Carlton Gibson, 8 weeks ago

@Jake I’d keep the scope as is. Let’s let folks build with the new tag (IMO)

comment:15 by Sarah Boyce, 8 weeks ago

Patch needs improvement: set

comment:16 by Jake Howard, 8 weeks ago

Patch needs improvement: unset

comment:17 by Sarah Boyce, 5 weeks ago

Patch needs improvement: set

comment:18 by Jake Howard, 5 weeks ago

Patch needs improvement: unset

comment:19 by Natalia Bidart, 5 weeks ago

Patch needs improvement: set

comment:20 by Natalia Bidart, 5 weeks ago

Patch needs improvement: unset

comment:21 by Natalia Bidart, 3 weeks ago

Patch needs improvement: set

I gave this another top-bottom pass. Once the minor nitpicks are fixed, I need to do a in-depth review in terms of test coverage and functionality.

comment:22 by Jake Howard, 8 days ago

Patch needs improvement: unset
Note: See TracTickets for help on using tickets.
Back to Top