Twig ternary operator, Shorthand if-then-else - php

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.

{{ (ability.id in company_abilities) ? 'selected' : '' }}
The ternary operator is documented under 'other operators'

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' : '' }}

Support for the extended ternary operator was added in Twig 1.12.0.
If foo echo yes else echo no:
{{ foo ? 'yes' : 'no' }}
If foo echo it, else echo no:
{{ foo ?: 'no' }}
or
{{ foo ? foo : 'no' }}
If foo echo yes else echo nothing:
{{ foo ? 'yes' }}
or
{{ foo ? 'yes' : '' }}
Returns the value of foo if it is defined and not null, no otherwise:
{{ foo ?? 'no' }}
Returns the value of foo if it is defined (empty values also count), no otherwise:
{{ foo|default('no') }}

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' }}

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' }}

Related

Laravel check if var is does not exist

I have condition:
{{ setting('notifications.email.enabled') ? 'checked' : '' }}
In setting('notifications.email.enabled') can be values: 1, 0 and values can does not exist. How I can check if setting('notifications.email.enabled') does not exist (in table), and if does not exist, then 'checked'. Now I get '' on does not exist.
try this:
{{ setting('notifications.email.enabled', true) ? 'checked' : '' }}
by setting a default value in the second parameter
Compare it with 0 and return checked in else case such that if it is not set or it is 1 then it will return checked.
{{ (setting('notifications.email.enabled') === 0) ? '' : 'checked' }}

How to use a variable in .twig syntax

I am to DRY in a twig template. I am wondering is you can access a variable in the {{ notation }} of twig.
For example:
{% if page.lang == 'en' %}
// do something
{{ content_en }}
{% endif %}
{% if page.lang == 'es' %}
// hacer algo
{{ content_es }}
{% endif %}
I tried some other approaches. But is it possible to (somehow) combine variables and do something like this:
// Php example
$var = 'content_';
$var .= page.lang;
// Output would be 'content_en';
But then for Twig?
// Something like
{{ content_ + page.lang }}
To make it more clear. I would like to access to correct variable.
$content_en = 'this is just some content';
$content_es ='Alguna informaciĆ³n';
$var = 'content_';
$var .= 'en';
$key = ${$var};
// Output is 'this is just some content'
echo $key;
If you want to concatenate the output you need to use ~
{{ 'content_' ~ page.lang }}
If you want to call a dynamic variable you need to use attribute
{{ attribute(_context, 'content_' ~ page.lang) }}
_context is a special variable in twig which containts all variables you passed towards the template
#Darabee.
You are correct, the syntax would be:
{{ attribute(_context, 'content_' ~ page.lang) }}
But how ever I noticed that if there is a array and u use '.' instead of a underscore '_' it will not work.
In my case I have a multi-array. That is the issue.
page['city']['content_en'].
or page.city.content_en

Laravel 4.2 blade: check if empty

In Laravel blade you can do:
{{ $variable or 'default' }}
This will check if a variable is set or not. I get some data from the database, and those variables are always set, so I can not use this method.
I am searching for a shorthand 'blade' function for doing this:
{{ ($variable != '' ? $variable : '') }}
It is hard to use this piece or code for doing this beacuse of, I do not know how to do it with a link or something like this:
{{ $school->website }}
I tried:
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
But, it does not work. And, I would like to keep my code as short as possible ;)
Can someone explain it to me?
UPDATE
I do not use a foreach because of, I get a single object (one school) from the database. I passed it from my controller to my view with:
$school = School::find($id);
return View::make('school.show')->with('school', $school);
So, I do not want to make an #if($value != ''){} around each $variable (like $school->name).
try this:
#if ($value !== '')
{{ HTML::link($value,'some text') }}
#endif
I prefer the #unless directive for readability in this circumstance.
#unless ( empty($school->website) )
{{ $school->website }}
#endunless
With php 7, you can use null coalescing operator. This is a shorthand for #m0z4rt's answer.
{{ $variable ?? 'default' }}
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
change to
{{ ($school->website != '') ? '' . $school->website . '' : '' }}
or the same code
{{ ($school->website != '') ? "<a href='$school->website' target='_blank'>$school->website</a>" : '' }}
{{ isset($variable) ? $variable : 'default' }}
I wonder why nobody talked about $variable->isEmpty() it looks more better than other. Can be used like:
#if($var->isEmpty())
Do this
#else
Do that
#endif
From Laravel 5.4, you can also use the #isset directive.
#isset($variable)
{{-- your code --}}
#endisset
https://laravel.com/docs/9.x/blade#if-statements

twig form_widget value variable doesn't work

{{ form_widget(form.category,{value:"3"}) }} //works!!
{{ form_widget(form.category,{value:'3'}) }} //works!!
{{ form_widget(form.category,{value:3}) }} // doesn't work !!
{{lastCatId}} // echos 3 !!!
{{ form_widget(form.category,{value:"lastCatId"}) }} //doesn't work ???
{{ form_widget(form.category,{value:'lastCatId'}) }} //doesn't work ???
I know after symfony 2.3. Value has to be quoted or double quoted. But I don't know why the variable doesnt work
never mind. I found the problem
{{ form_widget(form.category,{'value' : lastCatId|number_format }) }}
I had to cast it to int.

Is there a Twig shorthand syntax for outputting conditional text

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>
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' }}
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
The null-coalescing operator also working, like:
{% set avatar = blog.avatar ?? 'https://example.dev/brand/avatar.jpg' %}

Categories