Twig ternary operator, Shorthand if-then-else

Meliborn picture Meliborn · Aug 5, 2012 · Viewed 154.8k times · Source

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.

Answer

Ben Swinburne picture Ben Swinburne · Aug 5, 2012
{{ (ability.id in company_abilities) ? 'selected' : '' }}

The ternary operator is documented under 'other operators'