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

Volt loops produce identical iterator variables

Hi!

I suddenly ran into a problem with iterators. I have one loop in one file that does this:

{% for exercise in page.items %}

And inside that loop I call a partial:

<?php $this->partial("shared/view-exercise-in-list"); ?>

Inside the partial I have again a loop:

{% for exercise_name in exercise_names %}

And now the iterators generated by the two files:

<?php $v174897676652324318951iterated = false; ?><?php $v174897676652324318951iterator = $page->items; ...

and

<?php $v174897676652324318951iterator = $exercise_names; 

So the iterators are exactly the same. In fact, all iterators generated in one generator run (for the same request) all over the place are exactly the same. Iterators for another request are different but same between themselves. What could be the problem here?

I only noticed the problem when I removed the cache files. Apparently, the old generated files were correct but now the generation does not work properly and generates the same name for all iterators.

Thanks for ideas in advance,

Albert

edited Aug '17

Phalcon partials are scope-aware; you would have to pass in the iterated variable to the scope of the partial as arguments.

{% for item in page.items %}
    {{ partial('shared/view-exercise-in-list', ['exercise':item]) }}
{% endfor %}

Or, you could include it, that would expose the parent scope to the included template.

{% for item in page.items %}
    {% include 'partials/shared/view-exercise-in-list' %}
{% endfor %}

The problem is not that I cannot access the variable inside the Volt partial. The problem is that the loop inside the partial generates the same loop variable, thereby corrupting the loop variable outside the partial. What gives?

Ah, wait. So if I do not pass the $exercise into the partial it is not set, is that what you mean? That's probably new behavior in the newer versions... This used to work before. Okay, that makes sense because I check for the $exercise and if not set, I perform a DB lookup, getting a different $exercise in the process. That is useful to know. However, it still does not resolve the original question, does it?

Why does Volt generate the same variable name for the two loops? Isn't that a bug?



262

Do you have any suggestion about this issue since ? I can't find anything about it.

edited May '18

Hi!

I did solve the problem but it was a while... What I find in my code is this:

{% for exercise in page.items %}
...
<?php $this->partial("shared/view-exercise-in-list"); ?>

and then in the partial:

$exercise_names = $exercise->getExerciseNames()->toArray();
{% for exercise_name in exercise_names %}

So the code looks unchanged to me, in fact. That's weird. I wonder if something else was wrong... But I frankly ddo not remember. Sorry.

Albert



262
edited May '18

After some zephir debugging, I came across this old issue : Diffrent volt partial() in php 5.6/7.0 #12176 The loop context cannot be used 'atm' . Thanks.