¿Hay una sintaxis abreviada de Ramita para la salida de texto condicional


¿Hay una sintaxis más corta en Twig para generar una cadena de texto condicional?

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

Php tradicional es aún más fácil que esto:

<h1><?php info['id']? 'create' : 'edit' ?></h1>
 52
Author: Failpunk, 2012-11-12

2 answers

Esto debería funcionar:

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

También, esto se llama el operador ternario. Está algo oculto en la documenation: twig docs: operators

De su documentación la estructura básica es:

{{ foo ? 'yes' : 'no' }}
 114
Author: mcriecken,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-11 22:50:37

Si necesitas comparar el valor es igual a algo que puedes hacer:

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

Puede usar el operador de Elvis dentro de twig:

{{  user ? 'is-user' }} 

{{  user ?: 'not-user' }} // note that it evaluates to the left operand if true ( returns the user ) and right if not
 20
Author: Raja Khoury,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-11 17:59:30