How to unset a variable in Twig - twig

So I'm working with conditional rendering in a component reused at multiple place.
I have a twig variable my_var set as 'something'.
I want to unset it in my template.
Something like {% unset my_var %} the way this condition {% if not my_var %} would be fullfill.
I've tried to set it to false or null but this is not working anytips ?

Working here:
{% set my_var = false %}
{% if not my_var %}
not true
{% else %}
true
{% endif %}
twigfiddle

Related

How to find selected variation value as variable?

we´re using Shopware 6 and want to display the selected variation value after the title of the variation.
Example:
Variationname: Color
Variationvalue: Blue, Green, Black
If we select "Black" we want to display: "Color: Black". Whats the variable for our template?
I found the solution:
{% for option in group.options %}
{% set isActive = false %}
{% set isCombinableCls = 'is-combinable' %}
{% if option.id in page.product.optionIds %}
{% set isActive = true %}
{% endif %}
{% if isActive %}
{{ option.translated.name }}
{% endif %}
{% endfor %}

I want to put OR operator in twig if statement but OR operator not working in Twig

I have written following code but its not working -
{% for key, value in data %}
{% if key == "A" OR key == "A+B" OR key == "A+C" %}
{% set continue = "false" %}
{% endif %}
{% endfor %}
also tried
{% for key, value in data %}
{% if key == "A" || key == "A+B" || key == "A+C" %}
{% set continue = "false" %}
{% endif %}
{% endfor %}
But its not working. What am I doing wrong ?
Variables initiated inside a loop only live in the scope of that loop. U'd need to define the variable outside the loop in order to access it outside the loop.
{% set continue = true %}
{% for key, value in data %}
{% if key == "A" or key == "A+B" or key == "A+C" %}
{% set continue = false %}
{% endif %}
{% endfor %}
{{ continue ? 'continue' : 'dont continue' }}
demo
Note: the correct syntax of or in twig is or, nothing else.

What does Twig's 'default' filter do if no argument is provided?

I understand how the default filter would behave if it were used like so: items|default(posts)
However, I stumbled across some code where it was used but no arg was passed:
{% if ( posts|default ) %}
{% endif %}
It's possible that it's actually not doing anything and is just incomplete or boilerplate code, but I wanted to double-check.
Not passing any arguments the default filter will result in twig returning an empty string (''). It's also worth to mention that in twig, if you test an empty string it will result in false.
So in this case if the post variable is not defined, false or an empty string, the filter will return an empty string and the if will return the value false thus ignoring the code inside the code block
{% set foo = bar|default %}
{{ foo == '' ? 'empty string' : 'not an empty string' }}
{% if '' %}
Do something
{% else %}
Don't do anything
{% endif %}
--------------------------------
{% set var1 = false %}
{% set var2 = {} %}
{% if var1 | default %}
Do sthing with var1
{% else %}
Don't do anything with var1
{% endif %}
{% if var2 | default %}
Do sthing with var2
{% else %}
Don't do anything with var2
{% endif %}
{% if var3 | default %}
Do sthing with var3
{% else %}
Don't do anything with var3
{% endif %}
demo

Twig: how to set object variable?

I try to create new variable from exists object variable in Twig template (filter is object):
{% for filter in filters %}
{% if filter.type != 'selectImage' %}
{{ filter.render()|raw }}
{% elseif filter.type == 'selectImage'%}
{% set selectFilter = filter %}
{% endif %}
{% endfor %}
but i get error:
Catchable fatal error: Object of class Filters\Filter could not be converted to string in vendor\twig\twig\lib\Twig\Environment.php(403) : eval()'d code on line 40
on
{% set selectFilter = filter %}
How i can set object to new vairable?
According to the official TWIG documentation, filter is the name of a tag in TWIG language.
You should rename your variable in your code to avoid problems in the generated PHP code:
{% set selectFilter = myFilter %}

Scope of variable in jinja2 template

I am writing jinja2 template based application. I am trying to write a logic to set variable.
{% set last_item = none %}
{% for u in users %}
{% if not u.username == user.username%}
{% if g.user.is_bestfriend(u) %}
{% set last_item = 'true' %}
{% endif %}
{% endif %}
{% endfor %}
{{last_item}}
but after {% endfor %}, last_item value is again set to none, instead of true. Is there any way to set it to true in jinja2 template?
Since version 2.10 you can do this using a namespace variable, set before entering the scope:
{% set ns = namespace(found=false) %}
{% for item in items %}
{% if item.check_something() %}
{% set ns.found = true %}
{% endif %}
* {{ item.title }}
{% endfor %}
Found item having something: {{ ns.found }}
See also the documentation: http://jinja.pocoo.org/docs/2.10/templates/#assignments
Due to scoping rules in jinja2, you cannot access a variable outside the scope that it was set in. Sorry :(

Resources