【问题标题】:Does jinja support multiple blocks in a macro?jinja 是否支持宏中的多个块?
【发布时间】:2014-06-10 04:02:44
【问题描述】:

我正在使用带有 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 %}

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

【问题讨论】:

    标签: flask jinja2


    【解决方案1】:

    不,你不能。虽然您可以将多个参数传递给宏,但只能存在一个 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 %}
    

    【讨论】:

    • 谢谢@muffel!这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2012-09-16
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多