2011-07-05 36 views

回答

5

打開Twig中的調試模式。 使用調試擴展在2種情況下查看變量。

第一種方式

{% set car = 'Honda' %} 
{% debug car %} 

會告訴你,汽車仍然是一個字符串本田

然而,第二個辦法

{% set car %}Honda{%endset%} 
{% debug car %} 

會告訴你現在的車是

Twig_Markup Object([content:protected] => car)

因此,如果您想將它用作數組中的鍵或索引,請不要使用capture來設置變量。

更新:對於枝杈版本大於1.5使用dump更換調試

如:

{% set car = 'Honda' %} 
{% debug car %} 

如:

{% set car %}Honda{%endset%} 
{% debug car %} 
+0

@kissmyface h ttp://meta.stackexchange.com/questions/17463/should-i-ask-a-question-i-know-the-answer-to/17467#17467 –

+0

酷:-)謝謝 – calumbrodie

+2

看起來像調試已棄用在樹枝1.5。文檔建議使用dump而不是http://twig.sensiolabs.org/doc/functions/dump.html –

2

您也可以使用這樣的第二個方法(你應該修整車變量):

{% set car %}Honda{%endset%} 

{{ cars[car|trim].wheels | length }} 
相關問題