I have read the volt documentation in phalcon page and i cant find any example for this...
You can make easy loops in objects, for example, in php:
foreach($pages as $page){
echo $page->title;
}
in volts would be ...
{% for page in pages %}
{{ page.title }}
{% endfor %}
My question is, how i can make a normal numerical loop in volt? For example:
for($n=1;$n<10;$n++){
echo $n;
}
Thanks.
This will count from 1 to 10
{% for i in 1..10 %}
{{ i }}
{% endfor %}
Related
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
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 %}
Im trying in Twig to set different classes depending on how may items that gets rendered. Eg, if only one, if only two, if only three etc I want to set a different class on the item. How would I do that?
<ul>
{% for item in items %}
<li>
{% include 'components/list/person.twig' with item %}
</li>
{% endfor %}
</ul>
If you want to know in advice the length of the loop, you can use the predefined loop variable that is named length:
loop.length: The number of items in the sequence
as example:
{% for user in users %}
{{ loop.index }}/{{ loop.length }} - {{ user.username }}
{% endfor %}
hope this help
I am not sure that i understood your question or not...
But if you want to use different classes on base of index of loop then you can use loop.index in loop.
Hope this will help you
<ul>
{% for item in items %}
{℅ if loop.index == 1 ℅}
// Set class here
{℅ endif %}
<li>
{% include 'components/list/person.twig' with item %}
</li>
{% endfor %}
</ul>
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 %}
Hello I would like do something like that with my Twig template
<?php
for( $i = 0; $i <= 5; $i++ ) {
// Not display the first number
if( $i <= 1 ) {
continue;
}
// Displaying numbers from 2 to 5
echo $i ,'<br/>';
}
?>
How can I do that ?
Thanks for your help.
From the documentation you can use this to iterate numbers
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
Also from the documentation you can add conditions like this
<ul>
{% for user in users if user.active %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
So if you combine the two you end up with something like this.
{% for i in 0..5 if i<= 1 %}
* {{ i }}
{% endfor %}
Untested but should work.
THe documentation : http://twig.sensiolabs.org/doc/tags/for.html
If you literally just want to skip the first iteration, you can just do
{% for i in 1..5 %}
or
{% for i in 0..5 if loop.index0 %}
But assuming you want to actually do something more useful like some processing on the first iteration, then just only echo $i on subsequent iterations, this should work:
{% for i in 0..5 %}
This is printed every time...
{% if (loop.index0) %}
...but this is only printed when $i > 0: {{ i }}<br />
{% endif %}
{% endfor %}
There's not a "continue" keyword or any equivalent in Twig as far as I know.