views:

45

answers:

2

How can write tag "copyblock" for Django templates?

For such a functional::

<title> {% block title %} some title... {% endblock %} </title>
<h1>{% copyblock title %}</h1>

Thx!

+1  A: 

Take a look at the solutions mentioned in this question:

ars
+1  A: 

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 pushing and popping 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.

Jarret Hardie
+1: Focus on the `{{ title }}` and providing extra context. Custom template tags are rarely worth the effort.
S.Lott