Setting conditional value in twig do something block - symfony

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

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?

Twig - Get corresponding alphabet letter in loop

I want to use the loop.index variable in twig to get the corresponding alphabet letter (1 = A, 2 = B, etc).
{% for item in form.items %}
{% set nom_item = 'Item'~loop.index %}
{% endfor %}
How could I do to get alphabet letter in loop ? I can't find a twig function for that.
Simplest solution
{{ range('A','Z')[loop.index0] }}
try with this!
{% set foo = ['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'] %}
{% for index,item in form.items %}
{% set nom_item = 'Item'~foo[index] %}
{% endfor %}

Twig - Why are vars defined as false become empty?

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!

Twig loop gets mutliplied

Lets say I have some data called 'brands ' in an array past into a twig template like this:
{ 0:1,1:2,2:3,3:4,4:5,5:6,6:7,7:8}
Second array called 'designs' like this
{120:11,123:22,189:32,300:34,400:53,500:63,688:37,799:28}
o/p excepted
12,24,35,38,59,70,45,36
the below code shows what i have tried and the o/p expected for 8 times, but i am getting 64 times the output like
{% for key ,value in brands %}
{% for key,design in designs %}
// for example i need to add the value and design for 8 times
{% set total = value + design %}
{% endfor %}
{% endfor %}
You are incorrectly nesting your loops, and adding each element of designs to each element of brands. You only need one loop:
{% for key, value in brands %}
{% set total = value + designs[key] %}
{% endfor %}
Don't forget you can't access total unless you define it before starting the loop:
{% set total = 0 %}
{% for key, value in brands %}
{% set total = value + designs[key] %}
{% endfor %}
If you're looking for the sum of both of the arrays into one total with varying numbers of elements for brands and designs:
{% set total = 0 %}
{% for key, value in brands %}
{% set total = total + value %}
{% endfor %}
{% for key, value in designs %}
{% set total = total + value %}
{% endfor %}
If you're looking to add together each corresponding element between designs and brands without depending on matching keys - it's a much more difficult process when trying to do it in Twig, and you're missing what Twig is meant for entirely. You should handle this data in the controller or build a better data model to pass to Twig. For example:
$brands = array(0=>1,1=>2,2=>3,3=>4,4=>5,5=>6,6=>7,7=>8);
$designs = array(120=>11,123=>22,189=>32,300=>34,400=>53,500=>63,688=>37,799=>28);
$brands_designs = array();
foreach ($brands as $key => $brand) {
$design_key = key($designs);
$brands_designs[$key] = array(
'brand' => $brand,
'brand_key' => $key,
'design' => next($designs),
'design_key' => $design_key
);
}
return $this->render('AcmeBundle:Folder:template.html.twig', array('brands_designs' => $brands_designs));
Then in your Twig template:
{% for key, value in brands_designs %}
{% set total = value.brand + value.design %}
{% endfor %}
But if you insist on matching each element together and adding them individually...
{% for brandKey, brand in brands %}
{% set outerLoopIndex = loop.index %}
{% for designKey, design in designs %}
{% if outerLoopIndex == loop.index %}
{% set total = brand + design %}
{# do stuff with total here, like {{ total }} #}
{% endif %}
{% endfor %}
{% endfor %}
The above isn't tested, I'm a little worried about variable scope in there. It actually iterates a total of 64 times with your sample set but only outputs on the diagonal (when the outer index == inner index, so 1 == 1, 2 == 2, etc.)

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

Resources