Reputation: 5841
I would like a tag which would allow me to capture everything inside of it into a variable. Something like the following:
{% capture_to_var my_html %}
<a href="{% url foo %}">{{ var }}</a>
{% endcapture_to_var %}
After which the variable my_html
would contain an html snippet with the <a href...
This could be very handy when instantiating sub-templates via include
(e.g. {% include 'sub-item.html' with complex_html=my_html %}
). Django provides a way to do this via template inheritance, but that requires creating a separate file for each snippet, which is not nice when one has too many of them. Is there any way to do without that?
Upvotes: 0
Views: 548
Reputation: 1630
You can use a custom template tag, for example, this one:
https://djangosnippets.org/snippets/545/
Upvotes: 1