Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Twig shorthand syntax for outputting conditional text

Tags:

php

twig

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> 
like image 900
Justin Avatar asked Nov 11 '12 22:11

Justin


1 Answers

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' }} 
like image 83
mcriecken Avatar answered Oct 06 '22 09:10

mcriecken