Meliborn Meliborn - 6 months ago 102
Twig Question

Ternary operators in Twig php

Is it possible to use ternary operators in twig template? Now, for adding some class to DOM element depend on some condition I do like this:

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


Instead of

<tr class="<?=in_array($ability->id, $company_abilities) ? 'selected' : ''?>">


in native php template engine.

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

The ternary operator is documented under 'other operators'