We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Increment loop for more than 1 in volt

Could you please tell me how i do like this in volt ?

$x = 13;
for($i = 0; $i < $x ; $i = $i + 3) {
    echo '<div>';
    echo $i <= $x ? $i : '';
    echo $i <= $x - 1 ? ($i + 1) : '';
    echo $i <= $x - 2 ? ($i + 2) : '';
    echo '<hr><div>';
}

Thanks all pro so much

Can you check this if this will help you, its the same concept

https://stackoverflow.com/questions/21921126/numeric-loop-in-volt



93.7k
Accepted
answer
edited Jun '17

As @devninja86 suggested you will have to use the range operator .., but unfortunately it does not support the step parameter, which you need (at least I can't find a way to add it). Here is a solution, let's see if someone has better ideas :)

{% set x = 13 %}
{% set step = 3 %}
{% for i in range(0, x, step) %}
    <div>
        {{ i <= x ? i : '' }}
        {{ i <= x - 1 ? i + 1 : '' }}
        {{ i <= x - 2 ? i + 2 : '' }}
        <hr>
    </div> 
{% endfor %}

NOTE: For range PHP function to be available in Volt, you have to add it in your service definition:

$compiler->addFunction('range', 'range');

@devninja86 my problem is

$i = $i + 3

@Nikolay-Mihaylov range is a good idea.

Thank all answers.