Alexander Putilin
Alexander Putilin

Reputation: 2342

When inheriting flask/jinja2 templates, is it possible to add a new block?

Is it possible to add more blocks when inheriting templates in flask?

I have a base template from flask-bootstrap, and I would like to add a block to it(in my own base.html)

I've tried a straightforward approach(just simply adding more blocks to my base.html). However it seems like new blocks are ignored. I can simulate the desired behaviour by creating a nested block within an existing "content" block, but then I have to put {{super()}} boilerplate into every template.

Is there a way to just add more blocks?

Upvotes: 1

Views: 1124

Answers (1)

dirn
dirn

Reputation: 20729

You can add new blocks but they must be defined inside a block that exists in a parent template. For example, if content is defined in the extension's template

<body>
    {% block content %}{% endblock %}
</body>

You can then define blocks inside it within your base template

{% block content %}
    {% block header %}{% endblock %}
    {% block body %}{% endblock %}
    {% block footer %}{% endblock %}
{% endblock %}

Then in your page's template you can extend your base template and override its blocks as you need to.

Upvotes: 5

Related Questions