Twig ternary operator, Shorthand if-then-else

Viewed 213850

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.

5 Answers

If the price exists from the database for example then print (Price is $$$) else print (Not Available) and ~ for the concatenation in Twig.

{{ Price is defined ? 'Price is '~Price : 'Not Available' }}

I just used a as a general variable name. You can also use endless if else like this:

{{ a == 1 ? 'first' : a == 2 ? 'second' : 'third' }}
Related