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 %}
Related
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 ;-)
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(',') }}
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 loop multidimensional array in twig.
What I've tried is
{% for key, record in records %}
{% for key1, record1 in record %}
{{ key1 }}
{% endfor %}
{% endfor %}
But I only get the index of array like so
0
0
0
0
1
0
0
1
What I want is to get the value of MoneyChanger and AddTransaction Array. Like for example
id
name
currency
amount
pieces
I want to post my answer.
I manage to resolve the issue by
{% for key, record in records %}
{% for key1, record1 in record %}
{% if record1.client is defined %}
{{ record1.id }}
{% endif %}
{% endfor %}
{% 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 %}