Meliborn Meliborn - 1 year ago 208
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 in company_abilities%}
<tr class="selected">

Instead of

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

in native php template engine.

Answer Source
{{ ( in company_abilities) ? 'selected' : '' }}

The ternary operator is documented under 'other operators'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download