Looping on number value TWIG - php

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 %}

Related

Printing out an array in twig using a for loop

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 %}

Key inside Array won't parse correctly in TWIG

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 ;-)

Print Twig variable x times based upon randomized range

I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z

Is it possible to concatenate a variable to an if statement in Twig?

I am trying to concatenate a variable to an array key to get access to certain values in Twig, but no success so far.
I have a large PHP array that has for example keys like this:
$array = [
...
...
...
'test_1' => $test_1,
'test_2' => $test_2
];
I tried the following in my Twig template:
{% for i in 1..2 %}
{% if array.test_{{ i }} != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}
but that doesn't work.
Is there a way to access values like this in Twig?
Try this:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}

How do I make a simple count loop in Wordpress Timber(Twigg)?

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 %}

Categories