Is there a Twig shorthand syntax for outputting conditional text

前端 未结 3 1296
有刺的猬
有刺的猬 2021-02-02 04:51

Is there a shorter syntax in Twig to output a conditional string of text?

{% if not info.id %}create{% else %}edit{% endif %}

3条回答
  •  隐瞒了意图╮
    2021-02-02 05:28

    This should work:

    {{ not info.id ? 'create' : 'edit' }}
    

    Also, this is called the ternary operator. It's kind of hidden in the documenation: twig docs: operators

    From their documentation the basic structure is:

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

提交回复
热议问题