202

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.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Meliborn
  • 6,495
  • 6
  • 34
  • 53

5 Answers5

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

The ternary operator is documented under 'other operators'

Xavi Montero
  • 9,239
  • 7
  • 57
  • 79
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
  • 2
    This is very helpful in situations like marking a button or item as active for the current page. – Vahid Amiri Sep 01 '16 at 09:40
  • 1
    If TRUE, how do you go about printing ability.id? (in place of "selected"). – gdaniel Dec 07 '16 at 21:48
  • 2
    @gdaniel `{{ (ability.id in company_abilities) ? ability.id : '' }}` – Ben Swinburne Dec 07 '16 at 23:11
  • thanks. I have been having issues printing twig variables when inside inline conditionals. I will give this a try. – gdaniel Dec 08 '16 at 15:36
  • I'm wondering if this is supposed to be able to work inline to add an attribute value like a css class? It does not seem to work for me: `
    ` --_wanting to add a css class here based on the condition._
    – Jordan Aug 06 '19 at 19:17
  • @Jordan this is correct. If `model.event.eventDate` evaluates to true, it'll add the class. From twig's point of view, it does not know anything about classes, just prints strings, no matter if classes, IDs, raw HTML or whatever needed. Even more, it does not know about "printing". This is due to the `{{ }}`. It's just an expression that is evaluated as any other expression, like a sum, like a filter, like a method call. – Xavi Montero May 21 '22 at 09:48
139

You can use shorthand syntax as of Twig 1.12.0

{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}
{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
mgalic
  • 1,789
  • 1
  • 11
  • 7
120

Support for the extended ternary operator was added in Twig 1.12.0.

  1. If foo echo yes else echo no:

    {{ foo ? 'yes' : 'no' }}
    
  2. If foo echo it, else echo no:

    {{ foo ?: 'no' }}
    

    or

    {{ foo ? foo : 'no' }}
    
  3. If foo echo yes else echo nothing:

    {{ foo ? 'yes' }}
    

    or

    {{ foo ? 'yes' : '' }}
    
  4. Returns the value of foo if it is defined and not null, no otherwise:

    {{ foo ?? 'no' }}
    
  5. Returns the value of foo if it is defined (empty values also count), no otherwise:

    {{ foo|default('no') }}
    
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
8

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' }}
L3xpert
  • 1,109
  • 1
  • 10
  • 19
1

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' }}
WebMan
  • 360
  • 4
  • 14