我不懂Jinja2 Call Blocks

Tri*_*sie 11 python jinja2

我理解这个概念,但我不懂语法.

我将使用他们网站上使用的示例

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

什么是输出?

子问题(因为我对如何工作很困惑):你是否允许每个宏只有1个来电?

Mar*_*riy 12

这是输出:

<div class="dialog">
    <h2>Hello World</h2>
    <div class="contents">

   This is a simple dialog rendered by using a macro and
    a call block.

    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

因此,当我们调用render_dialog时,我们将'Hello World'作为标题caller()传递,当它到达时它会传递call块的内容.

  • 我发现在嵌套宏中运行`caller()`的嵌套宏给出了一个错误,因为没有传递给它的调用者数据.我真正想要的是传递给父宏的数据.我能够在外部宏中执行`{%set content = caller()%}`然后在内部宏中使用`{{content}}`. (4认同)