【问题标题】:Jinja2: saying 'Render this macro inside another macro or template'Jinja2:说“在另一个宏或模板中渲染这个宏”
【发布时间】:2013-03-25 16:28:49
【问题描述】:

我想在一个通用容器宏或模板中呈现许多宏。用伪代码:

宏1

宏2

宏3

容器

在模板中:

"render macro1 inside of Container" e.g. {{ macro1 with Container }}

我不想每次都通过渲染Container然后里面的宏,我只需要指定,当这个宏被另一个宏包围时

我认为 'call' (http://jinja.pocoo.org/docs/templates/#macros) 是我正在寻找的,但我现在不太明白。任何意见,如果不清楚,我如何澄清这一点。

【问题讨论】:

    标签: python templates macros jinja2


    【解决方案1】:

    这对我有用:

    {% macro _enclosure() %}
        <div id="topenclosure">hello I'm on top</div>
            {{ caller() }}
        <div id="bottomenclosure">hello I'm on the bottom</div>
    {% endmacro %}
    
    {% macro account_user_profile_macro(i) %}
        {% call _enclosure() %}
            {{i.__dict__}}
        {% endcall %} 
    {% endmacro %}
    

    【讨论】:

    【解决方案2】:

    来自documentation

    在某些情况下,将一个宏传递给另一个宏会很有用。为此,您可以使用特殊调用块。以下示例显示了一个利用调用功能的宏以及如何使用它:

    {% macro render_dialog(title, class='dialog') -%}
        <div class="{{ class }}">
            <h2>{{ title }}</h2>
            <div class="contents">
                {{ caller() }}
            </div>
        </div>
    {%- endmacro %}
    
    {% call render_dialog('Hello World') %}
        This is a simple dialog rendered by using a macro and
        a call block.
    {% endcall %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      相关资源
      最近更新 更多