【问题标题】:Twig shorthand conditionally set variableTwig 速记条件设置变量
【发布时间】:2021-03-07 02:16:50
【问题描述】:

从这个问题我们知道如何使用三元运算符来输出条件文本:Is there a Twig shorthand syntax for outputting conditional text

例子:

{{ foo ? 'yes' : 'no' }}

我们如何使用三元运算符有条件地设置一个变量,而不直接输出?

【问题讨论】:

    标签: php twig conditional-operator


    【解决方案1】:

    你可以使用:

    {% set foo = foo ? 'yes' : 'no' %}
    

    请注意,您需要使用{% %} 而不是{{ }} 并添加set 关键字。

    【讨论】:

      【解决方案2】:

      你试试

      {{ foo is defined ? 'yes' : 'no' }}
      

      {% if foo is defined %}
          {{ foo ? 'yes' : 'no' }}
      {% endif %}
      

      【讨论】:

        猜你喜欢
        • 2017-08-04
        • 2015-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多