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

Phalcon Partial Recursive

Hi, We are using Phalcon Partials with variables recursively. Since the Phalcon v3 update, we have the problem, that the variables of the parent partial is overwritten by the child partial. In v2 everything worked fine. As an example for our usecase:

initialization:

{{ partial("partials/category", ['category': category]) }}

partials/category.volt

{{ category.getId() }} //everything is fine

{{ partial("partials/category", ['category': category.getChildCategory()]) }}

{{ category.getId() }} //displays category id of child

Do you have any idea how we could prevent this? Thanks a lot



85.5k

i dont pass params to particials i just use them, but havent test it in volt



43.9k

Hi,

what'is strange here, is that this partial rendering change between v2 and v3.

But the hierarchical rendering process could expalin it and looks quiet logical: child partial is populated with "category:category.getChildCategory()", so calling after that data fetching category.getId() will naturaly give you the id of the child category.

My thought: create a copy of partials/category (say partials/chlidCategory) and pass him partial("partials/childCategory", ['childCategory': category.getChildCategory()])

And, finally, it is known as good mvc practice to populate data in the controller and the pass data to the view.



1.4k
Accepted
answer

Hi, @le51 thanks for your answer. Our problem why we can't implement a "parent partial" and a "child partial" is that we theoretically have an unlimited depth, so the parent category could have for example a child, a child-child and so on. We now solved the problem by implementing a method which renders the partial with a simple view. As a simple view doesn't override the variables of its parent simple view this works fine, although it isn't really code-wise a really nice solution. I still think it would be nice if you could partials recursively like you could in phalcon 2.x.