jinja2 - how to put a block in an if statement?

后端 未结 1 1106
予麋鹿
予麋鹿 2020-12-09 01:29

I am trying to use an if to determine which block should fill my {% block content %}.

I have a base.html which has a default <

相关标签:
1条回答
  • 2020-12-09 02:12

    You cannot make a {% block %} conditional; once you use the tag, the block is always going to be filled in.

    Put your conditional inside the block instead, and use super() to instruct Jinja to use the original contents of the block as defined in the template:

    {% extends "base.html" %}
    {% block content %}
        {% if condition %}
            <div>blah blah blah blah</div>
        {% else %}
            {{ super() }}
        {% endif %}
    {% endblock content %}
    
    0 讨论(0)
提交回复
热议问题