Entries Tagged ‘block content’
- If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.
- More {% block %} tags in your base templates are better. Remember, child templates don’t have to define all parent blocks, so you can fill in reasonable defaults in a number of blocks, then only define the ones you need later. It’s better to have more hooks than fewer hooks.
- If you find yourself duplicating content in a number of templates, it probably means you should move that content to a {% block %} in a parent template.
- If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped (see the next section), since
it was already escaped, if necessary, in the parent template.
- For extra readability, you can optionally give a name to your {% endblock %} tag. For example:
{% block content %}
…
{% endblock content %}
In larger templates, this technique helps you see which {% block %} tags are being closed.