Test a value with if - twig

I want to test in object, if a value is in a array.
products.name is a property of an object with as value the string 'AMUSE BOEUF'. I've tried the following snippet, but it's not working
{% if products.name == ['AMUSE BOEUF'] %}
GOLDEN PRIZE
{% endif %}
I also tried with a numerical like this and this is working,
{% if products.id == 1090 %}
WINNER IS 1090
{% endif %}
but what can i do for a string value?

Since ['AMUSE BOEUF'] is an array you should use the in operator instead:
{% if products.name in ['AMUSE BOEUF'] %}
GOLDEN PRIZE
{% endif %}
or don't use an array:
{% if products.name == 'AMUSE BOEUF' %}
GOLDEN PRIZE
{% endif %}
To be honest, you shouldn't hardcode such logic in templates anyway. This should be extracted to a service instead. For instance, you could develop your own twig test called won and write it like this, notice beautiful semantics:
{% if product has won %}
GOLDEN PRIZE
{% endif %}

Related

Opencart 3 TWIG: how to use AND operator

I'm trying to use the "AND" operator in TWIG in Opencart 3 and it doesn't work. Please tell me what I'm doing wrong.
I have some product attributes. And I want to make a condition that if two of the attribute with a specific ID are there, then the condition is met.
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 and attribute.attribute_id == 3 %}
First condition
{% elseif attribute.attribute_id == 2 %}
Second condition
{% elseif attribute.attribute_id == 3 %}
Third condition
{% else %}
{% endif %}
{% endfor %}
{% endfor %
Here is text example:
if there is an attribute with ID equal 2 and an attribute with ID equal 3 then write "Floor/Number of floors".
if there is an attribute with ID equal 2 only then write "Floor"
if there is an attribute with ID equal 3 only then write "Numbers of floors".
Something can't be both X and Y at the same time. Furthermore this is something I'd advice you to test in the controller and not in the view.
Anyway if you wanted to do this in the view you will need to track of the found attributes. You could do this with two booleans or just add a counter.
{% set cnt = 0 %}
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 or attribute.attribute_id == 3 %}
{% set cnt = cnt + 1 %}
{% endif %}
{% endfor %}
{% endfor %}
{% if cnt == 2 %}
{# do something #}
{% endif %}
You can simplify the if by using the test in
{% if attribute.attribute_id in [2, 3,] %}
Update of my answer because OP changed the requirements of the question
{% set words = [] %}
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 %}
{% set words = words|merge(['Floor',]) %}
{% elseif attribute.attribute_id == 3 %}
{% set words = words|merge(['Numbers of floors',]) %}
{% endif %}
{% endfor %}
{% endfor %}
{% if words|default %}
{{ words|join('/') }}
{% endif %}
demo

Twig strip textarea and create array

I'm trying to strip some text from a value and afterwards create an array from it with the stripped values.
I'm having troubles to strip the text value.
I can only do this on the frontend since I have no access to the backend (SaaS platform)
In below example value.value (originally a textarea) returns the following text:
[185047078]1x something - Type 1
[415533322]1x something - something
[152890667]1x something 500x500 mm
I want to strip the text so I have [185047078], [415533322], [152890667] left or without the brackets.
Normally in JS you would do something like:
hide_ids = txt.match(/[^\]\[]+(?=\])/g)
However it need to be done in Twig.
Afterwards I want to push the values into an array hide_ids.
{% set hide_ids = [] %}
{% if product.custom %}
{% for custom in product.custom %}
{% if 'Some title' in custom.title %}
{% for value in custom.values %}
{% set hide_this_id = value.value %}
{% if hide_this_id matches '{/[^\]\[]+(?=\])/g}' %}
{% set hide_ids = hide_ids | merge([hide_this_id]) %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% set hidden = false %}
{% if id in hide_ids %}
{% set hidden = true %}
{% endif %}
What is the equivalent of match in Twig? I also tried replace but I just can't get that text stripped.
Any help greatly appreciated!
You could go with some simple string functions.
{% for id in ids %}
{{ id | split(']', 2)[0] | replace({'[': '',}) }}
{% endfor %}
demo
split is the explode of twig. This will separate your string in chased based on the ] character. The 2nd parameter (2) ensures there will only be maximum 2 parts in the array.
replace is just str_replace
If you wanted to solve this with regex you would either need to write a function/filter with preg_match or install an extension like this

Find needle within haystack in twig

I want to check whether a certain word exists in a string. However, there's not other operator that exists which can do the job except containment operator. The following doesn't work to my utter frustration.
{% set deduction = 'Less Withholding Tax Thereon' %}
{% if 'Witholding' in deduction %}
{% set test = "with" %}
{% else %}
{% set test = "out" %}
{% endif %}
{{ test }}
Please see fiddle with various tests all amounting to same result
https://twigfiddle.com/00odoi

Check for paginate object in jinja2

I have a page which receives a results objects and is supposed to iterate over it. results can be a paginate object, in which case the iteration would be
{% for data in results.items %}
or it can be a list in which case we would iterate as
{% for data in results %}
I am now trying to distinguish between the two cases with
{% if results.items %}
{% for data in results.items %}
// do something with data
{% endfor %}
{% else %}
{% for data in results %}
// do something with data
{% endfor %}
{% endif %}
however, in my case it can happen that results.items == 0, which would mean that the if statement is false. Therefore I need to have a way to check whether results.items exists, independent of the value it has. Does anybody know how to do this?
it was easier than I thought...
{% if not results or (results.items is defined and not results.items) %}

How to use Twig_Markup object type in an If statement

I want to reuse pretty heavy logic only code a few times, in php I would use a function, but in twig I went with a solution from this old question.
In short, I use a macro like that:
{% import _self as test %}
{% macro check() %}
{{ test }}
{% endmacro %}
{% set v = test.check() %}
{% if v == 'test' %}
this should display
{% endif %}
Here is a fiddle: https://twigfiddle.com/kyv3zr/2
The problem is that v is a Twig_markup object. It doesn't seem to have any public properties. Running dump on it gives me this:
object(Twig_Markup)#1244 (2) { ["content":protected]=> string(13) " 1 " ["charset":protected]=> string(5) "UTF-8" }
How do I use it in an if statement?
Or is there a better way of storing a logic only code for reuse across templates?
If the object is called v then the dump seems to show it has a content value, so try:
{% if v.content == '1' %}
{# do something here #}
{% endif %}
not certain though, but try it.
EDIT #2 - based on comments question.
So I guess if you want to use v in an if statement, you would use it like so:
{% if v == '1' %}
{# do something here #}
{% endif %}
This presumes it does equal to "1".

Resources