是否有用于输出条件文本的 Twig 速记语法 [英] Is there a Twig shorthand syntax for outputting conditional text

查看:19
本文介绍了是否有用于输出条件文本的 Twig 速记语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Twig 中是否有更短的语法来输出条件字符串?

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

<h1>{% if not info.id %}create{% else %}edit{% endif %}</h1>

传统的 php 比这更容易:

Traditional php is even easier than this:

<h1><?php info['id']? 'create' : 'edit' ?></h1>

推荐答案

这应该有效:

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

另外,这被称为三元运算符.它有点隐藏在文档中:twig docs: operators

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' }}

这篇关于是否有用于输出条件文本的 Twig 速记语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆