2013-03-15 55 views
1

我用下面的代碼此刻生成標籤:的Jinja2和WTForms築巢

{{ form.code.label(class="control-label required") }} 

注所需的類。 現在這是硬編碼,但我想補充一點,動態地根據WTForms字段信息:

form.code.flags.required 

我想是這樣

{{ form.code.label(class="control-label {% if form.code.flags.required 
%}required{% endif %}") }} 

,但它並沒有奏效。 有沒有辦法解決這個問題?

回答

0

這個例子是行不通的,因爲使用Jinja2不會將「{{}}」括號內的語法爲模板的一部分,儘管它會嘗試一些聰明的解析,試圖弄清楚你想要什麼(詳細信息這裏:http://jinja.pocoo.org/docs/templates/#variables)。

的最簡單的方法是將外移動的邏輯:

{% if form.code.flags.required %} 
    {{ form.code.label(class="control-label required") }} 
{% else %} 
    {{ form.code.label(class="control-label") }} 
{% endif %}