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 %}
Related
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?
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!
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 %}
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
%}
I have different layouts depending on the user. This triggers the following error:
"Multiple extends tags are forbidden". How can I manage to use different layouts depending on the role of the user?
{% if is_granted('ROLE_USER_ONE') %}
{% extends "AcmeUserBundle::layout_user_one.html.twig" %}
{% elseif is_granted('ROLE_USER_TWO') %}
{% extends "AcmeUserBundle::layout_user_two.html.twig" %}
{% endif %}
EDIT
Here is the answer. I will use the case of 3 users in case people wonder how to do this. In this case admin has also userOne and userTwo privileges in case someone wonders about the else statement. I use Conditional Inheritance in this case, but as suggested in one of the answer, Dynamic Inheritance might be more readable.
{% set admin = false %}
{% set userOne = false %}
{% set userTwo = false %}
{% if is_granted('ROLE_ADMIN') %}
{% set admin = true %}
{% else %}
{% if is_granted('ROLE_USER_ONE') %}
{% set userOne = true %}
{% elseif is_granted('ROLE_USER_TWO') %}
{% set userTwo = true %}
{% endif %}
{% endif %}
{% extends admin ? "AcmeUserBundle::layout_admin.html.twig" : userTwo ? "AcmeUserBundle::layout_user_two.html.twig" : "AcmeUserBundle::layout_user_one.html.twig" %}
Check out the Conditional Inheritance section in the docs.
If you need more than two options, see the Dynamic Inheritance section:
{% set parent = 'defaultLayout.html.twig' %}
{% if is_granted('ROLE_USER') %}
{% set parent = 'userLayout.html.twig' %}
{% elseif is_granted('ROLE_ADMIN') %}
{% set parent = 'adminLayout.html.twig' %}
{% endif %}
{% extends parent %}
You should have two different templates
#user_one.html.twig
{% extends "AcmeUserBundle::layout_user_one.html.twig" %}
and
#user_two.html.twig
{% extends "AcmeUserBundle::layout_user_two.html.twig" %}
Then you should have one "entry" point - some user.html.twig, in which you'll decide:
#user.html.twig
{% if is_granted('ROLE_USER_ONE') %}
{% include "AcmeUserBundle::user_one.html.twig" %}
{% elseif is_granted('ROLE_USER_TWO') %}
{% include "AcmeUserBundle::user_two.html.twig" %}
{% endif %}