So this is only an example of what I'm trying to achieve.
I'm looping through all items and print only those, who starts with F and separate them with comma.So in the end, the loop will made, for example 15 iterations, but will print only 5.I'm trying to catch the last one that will be printed, so I can remove the comma.I tried with the filter loop.last, but it works only if the loop print the last item, but if the last print was earlier, it's still with comma.
{% for item in items %}
{% if item starts with 'F' %}
{{ item }},
{% endif %}
{% endfor %}
I can't edit anything from items.
Please help, I'v been stuck on this from a while.
A more simply solution could be Adding a condition to the for statement and display the comma only if is not the first interaction (loop.last is defined when using loop conditions). As example:
{% set items = ['Fitem1', 'item2', 'Fitem3', 'Fitem4', 'item5'] %}
{% for item in items if item starts with 'F'%}
{% if loop.first == false %},{% endif%}
{{item}}
{% endfor %}
See this twigfiddle for the working solutions
I tested this, so I thought I would provide an answer:
{% set items = ['Fitem1', 'item2', 'Fitem3', 'Fitem4', 'item5'] %}
{% set newArray = [] %}
{% for item in items %}
{% if item starts with 'F' %}
{% set newArray = newArray|merge([item]) %}
{% endif %}
{% endfor %}
{{ newArray|join(',') }}
Related
Suppose,
$a=1,2,3,4,5,6,7,8,9,10
then
in the for loop, it should run 1,2,3,4
and then 5 then 6,7,8,9 and then 10
then exit the loop.
You could use the batch and slice Twig filters for this. If you batch the results by groups of 5, then slice the first 4 and the 5th element you are able to loop through the results in the way you want.
Example code:
{% set items = [1,2,3,4,5,6,7,8,9,10] %}
{% for batch in items|batch(5) %}
<p>item 1 through 4</p>
{% for item in batch|slice(0, 4) %}
{{ item }}
{% endfor %}
<p>5th item</p>
{% for item in batch|slice(4, 1) %}
{{ item }}
{% endfor %}
{% endfor %}
i have to configurate a .csv file.
I have a variable that is either a single value or an array of values. If it is a single value, i want to put that single value into the cell, otherwise i want to put every value in the array into the cell.
This is my approach, but it's not iterating through the array. If the item is an array, it just puts nothing into to table cell. Do i need to increment i in some sort of way?
{% if item is iterable %}
{% for i in item[0..10]%}
*{{ item[i] }}
{% endfor %}
{% else %}
{{item}}
{% endif %}
Not sure why are you using {% for i in item[0..10]%} to iterate the item?
0..10 returns an array in twig, which will result in the following "code"
{% for i in item[Array] %}
This will return nothing as the index Array doesn't exist in the array item.
The proper way to iterate the value would be
{% if item is iterable %}
{% for i in item %}
{{ i }}
{% endfor %}
{% else %}
{{item}}
{% endif %}
If you are trying to display only 10 values from the array u'd go with
{% for i in item[0:10]%}
{{ i }}
{% endfor %}
or
{% for i in item|slice(0, 10) %}
{{ i }}
{% endfor %}
testplugin.testplugin.firstkey has following value 1, 2, 3
I have follwing code written in TWIG:
{% set key1 = [config("testplugin.testplugin.firstkey")] %}
{% for ids in key1 %}
{% set key1 = ids %}
GO-{{ ids }} {% if not loop.last %},{% endif %}
{% endfor %}
The problem is, that the config("testplugin.testplugin.firstkey") won't get parsed correctly. Actually it only gets parsed as one value instead of 3 seperat values in an array. But when i define the values manually - without a variable - it works as it should:
{% set key2 = [1, 2, 3] %}
{% for ids in key2 %}
{% set key2 = ids %}
GO-{{ ids }} {% if not loop.last %},{% endif %}
{% endfor %}
The first code does this:
GO-1, 2, 3
the second one looks like this (as it should):
GO-1, GO-2, GO-3
So my question is, why doeas the first code won't work properly?
I could figure it out by myself:
{% set key1 = config("testplugin.testplugin.firstkey"))|split(',') %}
{% for ids in key1 %}
{% set key1 = ids %}
GO-{{ ids }} {% if not loop.last %},{% endif %}
{% endfor %}
Thanks anyway ;-)
I would like to loop on this variable that I have created before :
{% set divisionElement = (elementsLength/2)|round|number_format(0) %}
The output of this is a number.
After that I would like to create a loop with this value like that :
{% for i in divisionElement %}
{{dump(i}}
{% endfor %}
When I tried to dump i in my loop I have nothing result.
Try using range, If divisionElement is > 0
{% for i in range(1, divisionElement ) %}
{{ i }},
{% endfor %}
How do I make a simple count loop in Wordpress Timber(Twigg)?
So basically just a loop like this:
($i = 0;0 < 3;i++){
echo $test[i];
}
You could use
{% for value in test %}
{{ value }}
{% endfor %}
that is safer than
{% for i in 0..2 %}
{{ test[i] }}
{% endfor %}
because in second version you have to care about index (is setted? and so on) whereas in the first you don't.
Of course if your final goal is to print only three elements from the array you should consider slice filter
{% for value in test|slice(0, 3) %}
{{ value }}
{% endfor %}