[英]Twig shorthand conditionally set variable
從這個問題我們知道如何使用三元運算符來輸出條件文本: Is there a Twig shorthand syntax for output of conditional text
例子:
{{ foo ? 'yes' : 'no' }}
我們如何使用三元運算符有條件地設置一個變量,而不直接輸出它?
您可以使用:
{% set foo = foo ? 'yes' : 'no' %}
請注意,您需要使用{% %}
而不是{{ }}
並添加set
關鍵字。
你試試
{{ foo is defined ? 'yes' : 'no' }}
或者
{% if foo is defined %}
{{ foo ? 'yes' : 'no' }}
{% endif %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.