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

Sha*_*haf 5 jinja2 flask

我正在使用带有忍者的烧瓶.

我知道您可以使用多个占位符块定义基页模板:

<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>
Run Code Online (Sandbox Code Playgroud)

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

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

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

muf*_*fel 5

不,你不能。虽然您可以向宏传递多个参数,但只能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 %}
Run Code Online (Sandbox Code Playgroud)