jinja是否支持宏中的多个块?

时间:2014-05-30 23:02:52

标签: flask jinja2

我正在使用带有jinja的烧瓶。

我知道你可以定义一个包含多个占位符块的基页模板:

<html>
    <head>
        [ standard meta tags, etc go here ]
        {% block css %}{% endblock %}
    </head>
    <body>
        [ standard page header goes here ]
        {% block content %}{% endblock %}
        [ standard page footer goes here ]
        {% block javascript %}{% endblock %}
    </body>
</html>

我知道您可以使用单个占位符定义宏:

{% macro dialog() %}
    <div class="dialog">
        [ standard dialog header ]
        {{ caller() }}
    </div>
{% endmacro %}

{% call dialog() %}
    <div class="log-in">
        Log in or sign up! (etc.)
    </div>
{% endcall %}

但是可以定义一个具有多个占位符块的宏吗?

1 个答案:

答案 0 :(得分:4)

不,你不能。虽然您可以将多个参数传递给宏,但只能存在一个caller。然而,您可以将参数从宏传递回调用上下文并模拟您想要的行为,如下所示:

{% macro twoblocks()%}
    <div class="content-a">
        {{ caller(True) }}
    </div>
    <div class="content-b">
        {{ caller(False) }}
    </div>
{% endmacro %}

{% call(isA) twoblocks() %}
    {% if isA %}
        A content
    {% else %}
        B content
    {% endif %}
{% endcall %}
相关问题