2011-02-15 38 views
9

我理解這個概念,但我不明白這個語法。我不明白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 %} 

會有怎樣的輸出example

子問題(因爲我很困惑這是如何工作的):你允許每個宏只有一個調用者嗎?

回答

10

這是輸出:

<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> 

所以,當我們調用render_dialog我們傳遞的「Hello World」作爲標題,當它達到caller()它通過call塊的內容。

+0

很酷。那麼,與在另一個宏中調用宏有什麼不同呢? – 2011-02-15 17:04:08