Reputation: 1581
I have a nested array that is compiled via the model in a CakePHP request. The array can be nested at a variable depth, and the content is to be returned to the user.
Currently the looping and rendering process for this array is completed using a function in the View element of the request. This function is first called at the base array's depth, and repeated for any array element that has further array children. eg.
function print_depth($elements) {
foreach($elements as $element) {
echo $element['title'];
if($element['children']) {
print_depth($element['children']);
}
}
}
print_depth($elements);
With this process, I can print out all levels of the array, whilst still keeping the markup flexibility within the view (hence, it is skinnable), but I imagine this is the incorrect location for the function that handles this.
Is there a more MVC-valid process for this operation?
Upvotes: 1
Views: 776
Reputation: 25698
Create a helper instead of putting a single function in the view code.
What you do sounds like rendering a tree, there is already a tree helper out there.
https://github.com/CakeDC/utils/blob/master/View/Helper/TreeHelper.php
Upvotes: 1
Reputation: 60453
From an MVC point of view it's probably right where it belongs, since it's the views responsibility to prepare data for visual representation. However for a more DRY approach, you could implement this functionality in a helper.
Upvotes: 2