php - Twig 三元运算符,简写 if-then-else

标签 php twig conditional-operator

Twig 是否支持三元(if-else 简写)运算符?

我需要一些条件逻辑,例如:

{%if ability.id in company_abilities %}
    <tr class="selected">
{%else%}
    <tr>
{%endif%}

但在 Twig 中使用简写。

最佳答案

{{ (ability.id in company_abilities) ? 'selected' : '' }}

三元运算符记录在 ' other operators 下'

关于php - Twig 三元运算符,简写 if-then-else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820297/

相关文章:

php - 在输入表单中显示来自不同表的字段

javascript - Twig json_encode 与实体对象和集合(Symfony3)

html - 以 Symfony 形式编辑复选框列表的 View

sql - Oracle 中的 if(condition, then, else)

php - 检查消息是否已读和未读

php - 一个PHP模板页面,超链接调用不同表

php - 电子商务产品过滤器数据库设计

twig - 在 Twig (Timber) 中调用 Helper 函数

java - 静态数组初始化中的三元运算符未按预期工作

mysql - 使用 MySQL 为交叉表提取第二个值