I am writing a unit test with Codeception to test a twig template. However, I am unable to find a way to mock the block.richTextFields.all() function. The block.richTextField is passed by the Twig context in this case so my initial thought was to create TwigFunction to mock it, but I can't find a way to have all function inside block.richTextField namespace.
{% for text in block.richTextFields.all() %}
{% if (loop.length < 3 and loop.length > 1 ) or (loop.length == 4) or (loop.length < 3 and loop.index == 2) or (loop.length < 6 and (loop.index == 4 or loop.index == 5)) %}
{% set topRowColWidth = "col-lg-6 margin-top-10" %}
{% elseif loop.length == 1 and loop.last %}
{% set topRowColWidth = "col-lg-12 margin-top-10" %}
{% else %}
{% set topRowColWidth = "col-lg-4" %}
{% endif %}
<div class="{{ topRowColWidth }} dynamic-rich-text">
{% if text.richTextField|length %}
{{text.richTextField}}
{% endif %}
</div>
{% endfor %}
My goal is having a string like "name: smoothie, ingredients: milk, orange, category: vegan" (there can be a lot option: value or option: value, value, value... pairs) to produce an array like the following
[
{
option: 'name',
value: ['smoothie']
},
{
option: 'ingredients',
value: ['milk', 'orange']
},
{
option: 'category',
value: ['vegan']
}
]
I thought something like the following would work but it produces the following error and i can't understand why.
The problem is in the line where i try to add a value to the existing options[last_ok_index].value array
{% set options[last_ok_index].value = options[last_ok_index].value|merge( [ x[0] ] ) %}
Unexpected token "punctuation" of value "[" ("end of statement block" expected).
{% set product = "name: smoothie, ingredients: milk, orange" %}
{% set options = [] %}
{% set last_ok_index = 0 %}
{% for item in product|split(',') %}
{% set x = item|split(': ') %}
{% if x|length == 2 %}
{% set options = options|merge( [ { option: x[0], value: [x[1]] } ] ) %}
{% set last_ok_index = loop.index - 1 %}
{% else %}
{% set options[last_ok_index].value = options[last_ok_index].value|merge( [ x[0] ] ) %}
{% endif%}
{% endfor %}
{# print result #}
{% for item in options %}
{{item.option }}
{% for inner_item in item.value %}
{{"-" ~ inner_item}}
{% endfor %}
{% endfor %}
You should go with the suggestion in the comments by #dbrumann and use a TwigExtension.
However if you want to solve this in pure twig, then you are overcomplicating things.
First things first, the problem already starts at your first split, your expected output is smoothie and ingredients, while the actual result will be smoothie, ingredients, orange. You can fix this by passiung asecond argument to the split filter, which will limit the output.
Split uses the PHP function explode in the background. More on what the second parameters does you can find in the documentation here
Now as I said you can simply your snippet by creating the "item" in two parts rather than one part
{% set product = "name: smoothie, ingredients: milk, orange" %}
{% set items = [] %}
{% for item in product|split(',', 2) %}
{% set tmp = item|split(':') %}
{% set option = tmp[0] %}
{% set values = [] %}
{% for value in tmp[1]|split(',') %}
{% set values = values|merge([ value, ]) %}
{% endfor %}
{% set items = items|merge([ {'option': option, 'values': values,}, ]) %}
{% endfor %}
demo
As you've changed the initial input of the original question. The problem still starts with the split filter. I'd suggest you use another delimeter for your values, e.g. ;
{% set product = 'name: smoothie 3, ingredients: milk ; orange; pineapple, category: bar' %}
{% set products = [] %}
{% for string in products_raw %}
{% set product = [] %}
{% for item in string|split(',') %}
{% set tmp = item|split(':') %}
{% set option = tmp[0] %}
{% set values = [] %}
{% for value in tmp[1]|split(';') %}
{% set values = values|merge([ value, ]) %}
{% endfor %}
{% set product = product|merge([ {'option': option, 'values': values,}, ]) %}
{% endfor %}
{% set products = products|merge([ product, ]) %}
{% endfor %}
{% for product in products %}
{% for item in product %}
- Option: {{ item.option }}
- Values:
{% for value in item.values %}
- {{value }}
{% endfor %}
{% endfor %}
----------------------------------
{% endfor %}
demo
Thank you a lot for all the tips, i changed the logic a bit and it works now.
I will search about twig extensions as you proposed since it is for sure too much code in twig for something like that.
{% set product = "name: smoothie, ingredients: milk, orange, sugar, tags: healthy, popular, category: milk" %}
{% set options = [] %}
{% set last_option = null %}
{% set last_value = null %}
{% for item in product|split(',') %}
{% set x = item|split(':') %}
{% if x|length == 2 %}
{% if last_value|length > 0 %}
{% set options = options|merge( [ {option: last_option, value: last_value} ] ) %}
{% endif %}
{% set last_option = x[0] %}
{% set last_value = [x[1]] %}
{% else %}
{% set last_value = last_value|merge([x[0]]) %}
{% endif%}
{% if loop.last %}
{% if last_value|length > 0 %}
{% set options = options|merge( [ {option: last_option, value: last_value} ] ) %}
{% endif %}
{% endif %}
{% endfor %}
{# print result #}
{% for item in options %}
{{ item.option }}
{% for inner_item in item.value %}
{{ "-" ~ inner_item }}
{% endfor %}
{% endfor %}
How to render twig condition dynamically from php array
Php data array
arry('label'=>'test','parentRoleExp'=>"is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG')"
twig code
{% set parentRoleExp = '' %}
{% if link['parentRoleExp'] is defined %}
{% set parentRoleExp = link['parentRoleExp'] %}
{% endif %}
{% if parentRoleExp %}
<h1>Admin Blog</h1>
{% else %}
<h1>Blog <?h1>
{% endif %}
Above code not working
Expected result
{% if is_granted('ROLE_ADMIN') OR is_granted('ROLE_BLOG') %}
<h1>Admin Blog</h1>
{% else %}
<h1>Blog <?h1>
{% endif %}
I tried with various method but not working. please help
Thanks in advance
it's very bad idea - ulnerability
But if you want... register eval() function:
$twig = new Twig\Environment($loader);
$twig->addFunction(new Twig\TwigFunction('phpEval', 'eval'));
in template
{% if phpEval(parentRoleExp) %}
Currently I have two arrays
{% set code = [AMS, EIN, RTM] %}
{% set city = [Amsterdam, Eindhoven, Rotterdam] %}
I would like to check if the value of {{airport}} exists in the first array and if it is code[0] I would like to change {{airport}} into the value of city[0]. Is this possible with Twig?
You can loop over the code array:
{% for c in code %}
{# ... #}
{% endfor %}
Documentation: https://twig.symfony.com/doc/2.x/tags/for.html
Then if the item does match:
{# ... #}
{% if airport == c %}
{# ... #}
{% endif %}
{# ... #}
Documentation: https://twig.symfony.com/doc/2.x/tags/if.html
Replace the variable airport, at the same loop index:
{# ... #}
{% set airport = city[loop.index0] %}
{# ... #}
Documentation: https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
So, in full:
{% for c in code %}
{% if airport == c %}
{% set airport = city[loop. index0] %}
{% endif %}
{% endfor %}
Running fiddle: https://twigfiddle.com/xflfas/2
Out of the scope note: your arrays would be better named cities and codes.
This way, when you loop over them, you end up with meaningful naming
{% set codes = ['AMS', 'EIN', 'RTM'] %}
{% for code in codes %}
{{ code }}
{% endfor %}
{# and #}
{% set cities = ['Amsterdam', 'Eindhoven', 'Rotterdam'] %}
{% for city in cities %}
{{ city }}
{% endfor %}
Use {% if value in array %} to search from the first array and Twig's merge function to replace the value in the second array. See this https://twig.symfony.com/doc/2.x/filters/merge.html
We are making a website with a search form. In that form users can search with text and checkboxes. With the checkboxes users can search only in the related categories.
We need to accomplish this with Craft CMS and Twig.
We tried the following method, but that didnt worked.
{% set query = craft.request.getParam('search-results') %}
{% set nietBijdragePlichtig = craft.request.getParam('plg-Nee') %}
{% if nietBijdragePlichtig == 'on' %}
{% for entry in craft.entries.section('producten').limit(null).order(asc).search('query, nietBijdragePlichtig').find() %}
<div id=”test”>test</div>
{% endfor %}
{% endif %}
and we tried:
{% set query = craft.request.getParam('search-results') %}
{% set nietBijdragePlichtig = craft.request.getParam('plg-Nee') %}
{% set alle = query ~ ' ' ~ nietBijdragePlichtig %}
{% if nietBijdragePlichtig == 'on' %}
{% for entry in craft.entries.section(‘producten’).limit(null).order(asc).search(alle).find() %}
<div id=”test”>test</div>
{% endfor %}
{% endif %}
We also tried to use multiple .search()
We tried both codes with different syntaxes.
How can we accomplish this?
Really thanks!