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

2022-01-22 00:00:00 conditional-operator php twig

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

Does Twig support ternary (shorthand if-else) operator?

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

I need some conditional logic like:

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

但在 Twig 中使用简写.

but using shorthand in Twig.

推荐答案

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

三元运算符记录在 '其他运算符'

The ternary operator is documented under 'other operators'

相关文章