#35535: Simple block tag
-------------------------------------------+-----------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: Template system | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+-----------------------------
Django's template system allows creating custom tags, but they don't allow
taking content:
{{{#!django
{% 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:
{{{#!django
{% 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
[
https://github.com/django/django/blob/main/django/templatetags/cache.py
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:
{{{#!python
@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.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35535>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.