Zakaria Talhami
Zakaria Talhami

Reputation: 116

Django Template overriding

I was wondering, when using templates in django, I'm able to extend other base templates and override some of the blocks in the base template. so my question is when I override, would the code in the overridden block still get rendered then overridden, or would it never be run and only the new block is rendered?

Example:

base.html

{% block menu %}
    {% for option in menu %}

       ...Create the menu entries

    {% endfor %}
{% endblock menu %}

extender.html

{% extends base.html %}

{% block menu %}
    ... some other tags
{% endblock menu %}

In this case does the original for loop in the base.html run if it gets overridden?

Upvotes: 2

Views: 375

Answers (1)

Gabriel
Gabriel

Reputation: 1149

As far as I know the block will be overwritten unless you want to preserve its code in your extended template.

If you want to preserve the original block you can use {{ block.super }}

base.html

[...]
<body>
    {% block header %}
        base header content
    {% endblock %}
    [...]
</body>

extended.html

{% extends "base.html" %}
{% block header %}
    {{ block.super }}
    new content added
{% endblock %}
[...]

G.

Upvotes: 2

Related Questions