I am writing a custom tag to create dynamic css and other html formating around container html blocks. Basically, the idea is I want some the boilerplate HTML to be nested around the “meat” of the html blocks. For example, let’s say I have a table, I then want to nest some divs (or possibly other things) around that. These divs are not always the same, but generally the same.
What I want is for the closing tag, as an example, {% endtag %} to also do something.
The beginning of the tag seems easy enough and can be done using inclusion.
To demonstrate my idea above, here is some HTML without the tag:
<div class=someclass>
<table class=someclass id=someid>
stuff here
</table>
</div>
Instead, using the tag, it would look like this:
{% customtag arg %}
<table class=someclass id=someid>
stuff here
</table>
{% endcustomtag %}
In the demonstration above customtag and endcustom tag each render the appropriate portions of the div.
In short:
Is it possible to use {% endXXX %}, or some other mechanism, to render HTML at the end of the block?
You can!
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#parsing-until-another-block-tag
Have a look at a tag we’ve created, using the end tag:
https://github.com/pythonheads/inc/blob/master/src/inc/templatetags/inc.py#L97