簡體   English   中英

Twig 速記有條件地設置變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM