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}"
I raised a ticket about this, but was pointed here as a better way of collecting interest. I already have a working demo of this working.