Search code examples
phptwigconditional-operator

Twig ternary operator, Shorthand if-then-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%}

but using shorthand in Twig.


Solution

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

    The ternary operator is documented under 'other operators'