How can write tag "copyblock" for Django templates?
For such a functional::
<title> {% block title %} some title... {% endblock %} </title>
<h1>{% copyblock title %}</h1>
Thx!
How can write tag "copyblock" for Django templates?
For such a functional::
<title> {% block title %} some title... {% endblock %} </title>
<h1>{% copyblock title %}</h1>
Thx!
Django's template parser doesn't expose blocks by name. Instead, they are organized into a tree structure in the Django Template
's nodelist
, with rendering push
ing and pop
ping on the stack of template nodes. You'll have a nearly impossible time accessing them in the way your example indicates.
The SO link that ars references provides suggestions on the best solutions. Of those solutions, defining a variable in the context (ie: {{ title }}
in the spirit of your example) that can be reused is probably the most straightforward and maintainable approach. If the piece you want to duplicate goes beyond a simple variable, a custom template tag is probably the most appealing option.