82

Is there a shorter syntax in Twig to output a conditional string of text?

<h1>{% if not info.id %}create{% else %}edit{% endif %}</h1>

Traditional php is even easier than this:

<h1><?php info['id']? 'create' : 'edit' ?></h1>
Justin
  • 4,203
  • 7
  • 41
  • 58
  • Possible duplicate of [Ternary operators in Twig php (Shorthand form of if-then-else)](https://stackoverflow.com/questions/11820297/ternary-operators-in-twig-php-shorthand-form-of-if-then-else) – Peyman Mohamadpour Apr 16 '18 at 12:56

3 Answers3

173

This should work:

{{ not info.id ? 'create' : 'edit' }}

Also, this is called the ternary operator. It's kind of hidden in the documenation: twig docs: operators

From their documentation the basic structure is:

{{ foo ? 'yes' : 'no' }}
mcriecken
  • 3,217
  • 2
  • 20
  • 23
  • 1
    It looks like "if" will not work since the code is not wrapped in the statement delimiter {% %}. Omitting "if" averts the exception above. – Justin Nov 11 '12 at 22:47
  • You're right, I just did a test here and the 'if' is implicit. – mcriecken Nov 11 '12 at 22:49
  • 1
    problem if foo not defined – khaled_webdev May 30 '14 at 08:55
  • 8
    In a ternary why would you ever do this? {{ not info.id ? 'create' : 'edit' }} Just apply the logic in a straightforward manner. {{ info.id ? 'edit' : 'create' }} The 'not' operation just confuses the issue - you have to specify both outcome strings anyway, why make the code ever so slightly harder to read by doing negation when not needed? – Michael Morris Jul 29 '16 at 15:36
  • `{{ foo_val ? foo_val : (bar_val ? bar_val : "default") }}` also works, not the prettiest thing, though. – vbarinov Jan 10 '17 at 22:17
  • 1
    Yes, this works! Perfect for input preset value validation (`value="{{ person.curp is not null ? person.curp : '' }}"`) – Fernando León Sep 22 '19 at 20:28
42

If you need to compare the value is equal to something you can do :

{{  user.role == 'admin' ? 'is-admin' : 'not-admin' }}

You can use the Elvis Operator inside twig :

{{  user ? 'is-user' }} 

{{  user ?: 'not-user' }} // note that it evaluates to the left operand if true ( returns the user ) and right if not
Raja Khoury
  • 3,015
  • 1
  • 20
  • 19
  • The so called Elvis operators don't work. I get an error about wrapping the island in parens – TheRealChx101 Apr 30 '20 at 17:26
  • @TheRealChx101 not sure what you mean by `wrapping the island in parens` - could you share your example? – Raja Khoury May 03 '20 at 02:32
  • Wrapping the IF code island in parentheses. I think it could be the version I'm using. It's actually JTWIG (Java version) but that shouldn't matter much. – TheRealChx101 May 03 '20 at 19:11
  • not familiar with Java, it could be related to your framework/jtwig but I doubt it. its worth mentioning although all answers to this questions are correct i prefer to use a normal if/else statement most of the times, does that work for you? sorry I can't help much here.. good luck! – Raja Khoury May 04 '20 at 22:36
  • if you're curious about the `code island parens` referred to above - it's a jtwig thing, and is about using `{% ... %}` https://jtwig.gitbooks.io/jtwig-reference-manual/content/syntax/basic.html – Sandra Nov 09 '21 at 17:08
5

The null-coalescing operator also working, like:

{% set avatar = blog.avatar ?? 'https://example.dev/brand/avatar.jpg' %}
danigore
  • 411
  • 1
  • 5
  • 8