Twig - Why are vars defined as false become empty? - symfony

Define a twig var as true, and it has a value of '1'.
Define another var as false, and it has no value. Why is that?
{% set myOtherVar = true %}
{{ myOtherVar }}
This outputs '1'
{% set myVar = false %}
{{ myVar }}
This outputs nada
https://twigfiddle.com/ebbwgf
This is bothersome because simple logic like this fails:
{% set myVar = false %}
{% if myVar is not empty and not myVar %}
stuff
{% endif %}

false is equivalent to '' for empty:
{% set myVar = '' %}
{% if myVar is empty %}
stuff
{% endif %}
{% set myOtherVar = false %}
{% if myVar is empty %}
other stuff
{% endif %}
https://twigfiddle.com/5g9thl
Depending on what you want to do, you can test if the variable exists with defined:
{% set myVar = false %}
{% if myVar is defined and not myVar %}
stuff
{% endif %}
https://twigfiddle.com/m1n1q0

Because you are using set to set the variable as a boolean. So you need to use it as a boolean. You can't print the value.
You might do your logic something like this:
{% set myOtherVar = true %}
{% set myVar = false %}
{% if not myVar %}
myVar is false
{% endif %}
{% if myOtherVar %}
myOtherVar is true
{% endif %}
Here is a twigfiddle showing it working properly:
https://twigfiddle.com/yxt1jd
Hope that helps!

Related

Is there a way to define logic in a block rather than in a bunch of individual tags?

For Example I have quite a lot of logic that looks like this
{% set allowed_observations = ["PAR;101;ZN_GYM", "PAR;101;ZN_CARDIOGYM", "PAR;101;0;G15"] %}
{% set allowed_observations_names = ["Downstairs Gym", "Upstairs Gym (Cardio)", "Swimming Pool"] %}
{% for observation in observations %}
{% if (observation.location.locationCode in allowed_observations) %}
{% if ("HeadCount" in observation.observationTag) %}
{% set name = "" %}
{% for key,value in allowed_observations %}
{% if (value == observation.location.locationCode) %}
{% set name = allowed_observations_names[key] %}
{% endif %}
{% endfor %}
{% set data = data | push({
name: name,
location: observation.location.locationCode,
value: observation.value
}) %}
{% endif %}
{% endif %}
{% endfor %}
I was wondering if you can just do a single {% ...all code... %} type thing, using ; for line breaks or something?

Symfony : "if" does not work in a twig template

I'm trying to validate the size of an array before print a value, but the if instruction doesn't work. Always pass thought the if.
This is my code:
{% set size = custodian.phoneNumbers|length %}
{% if size > 3 %}
{% block phone_number3 %}{{phoneNumbers[2].phoneNumber }}{% endblock %}
{% endif %}
size is equal to 2
I try with this code and does not work as well.
{% set size = true %}
{% if size == false %}
{{size}}
{% endif %}
Please help!!!
Thanks in advance.
I found the answers myself
The block should be outside of the if.
{% block phone_type3 %}
{% if size >= 3 %}
{{ custodian.phoneNumbers[2].phoneType.value }}:
{% else %}
:
{% endif %}
{% endblock %}
{% block phone_number3 %}
{% if size >= 3 %}
<b>{{ custodian.phoneNumbers[2].phoneNumber }}</b>
{% endif %}
{% endblock %}

Setting conditional value in twig do something block

I am trying to setting a conditional value using if and else if inside a twig template, so that is what I have to do
{% set x = '1' %}
{% if a and b %}
{% set x = '2' %}
{% elseif a or b %}
{% set x = '3' %}
{% else %}
{% set x = '4' %}
{% endif %}
The above works fine for me. But my question is, is there a way I can achieve the same using only one {% %} (do something) block.
Something like:
{%
set x = '1'
if a and b
...
elseif a or b
...
else
%}

Set simple boolean in twig

lets say i have some data like this :
answers: [
{
answerText: "please",
small: false
},
{
answerText: "help",
small: true
},
{
answerText: "me",
small: false
}
],
and i want to set a boolean that´s true if there is an answer where small is true.
and i need to use it outside the loop i´m iterating over answers.
im trying arround and just dont get it, i think my nearest attempt is sth. like this
{% set zyx = if 'small' in question['answers'] %}
{% set zyx = 'small' in question['answers'] %}
{% set zyx = 'small:true' in question['answers'] %}
{% set zyx = true in question['answers'] %}
but they all dont work as i expect
for any help thanks in advance
You cannot use statements in an expression. Removing if will do the trick:
{% set zyx = 'small' in question['answers'] %}
EDIT
To check if answer.small is true, use:
{% set zyx = question['answers']['small'] is true %}
In my case,
for setting/initializing the variable value i wrote,
{% set status = false %}
and for checking
{% for item in items %}
{% if status is not true %}
// do something
{% if item.something is true %}
// do something
{% set status = true %}
{% elseif item.something is false %}
// do something
{% set status = false %}
{% endif %}
{% endif %}
{% endfor %}
{% set smallText = null %}
{% for answer in questions.answers %}
{% if answer.small %}
{% set smallText = answer.answerText %}
{% endif %}
{% endfor %}
{% if smallText is not null %}
Small answer: {{ smallText }}
...
{% endif %}

Twig: How to get the first character in a string

I am implementing an alphabetical search.
We display a table of Names. I want to highlight only those alphabets, which have names that begin with the corresponding alphabet.
I am stumped with a simple problem.
How to read the first character in the string user.name within twig.
I have tried several strategies, including the [0] operation but it throws an exception.
Here is the code
{% for i in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0-9'] %}
{% set has_user_starting_with_alphabet = false %}
{% for user in pagination %}
{% if user.name[0]|lower == i %}
{% set has_user_starting_with_alphabet = true %}
{% endif %}
{% endfor %}
{% if has_user_starting_with_alphabet %}
<li><span>{{ i }}</span></li>
{% endif %}
{% endfor %}
Is there some function like "starts_with" in twig?
Since twig 1.12.2 you can use first:
{% if user.name|first|lower == i %}
For older version you can use slice:
{% if user.name|slice(0, 1)|lower == i %}
Note: You may also use this notation:
{% if user.name[:1]|lower == i %}

Resources