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:
There are several in this space. The danger is we get into a never ending discussion about which features to add. (This happens a lot )
Simple, to the point, covers the 80% case is probably enough for an addition to Django itself here, leaving space in the ecosystem for folks to explore more advanced options.
Absolutely! We can reuse much of the implementation of simple_tag to keep many of the same semantics. Validating the overlap of the content argument is something I’d missed, but assuming simple_tag does the same, I should be able to share some of the implementation.
It was in a private repo when I opened the ticket, but I recently pushed up a branch to start integrating it with Django core. It’s still very rough, and missing “important” things like tests, but the basics are there: GitHub - RealOrangeOne/django at 35535-simple-block-tag. Hoping to get around to finishing it off and writing the related in the next week or so to get a draft PR up.