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

Best way to output recursive content

Hello,

I have in the database recursive data, that means container containing other container and/or normal data to output. What is the best way to organize my controller and volt files to handle this?

Each container should be a <div class="container-###"> with its content. Content could be other container again with its own content and so on.

There are different containers, so different html templates should be used. The information about the template is also stored in the DB, so I could use $this->view->pick() to select the corresponding .volt file.

I have the class ContentController::viewAction which shows me one container. I have already logic for recursion, but I'm not sure where to put it, to have everything right.

Thanks

I'd go for partials... it sounds like you only need logic to pick the volt path, so you don't need any logic in controllers.

edited Dec '16

Found this in old project, you can easily rewrite it with Volt.

<?php       
    function recursiveCategoryTree($items)
    { 
        foreach ($items as $item) {
            echo '<ul>';
                echo '<li>';
                    echo '<b">'. $item['title'] .'</b>';
                    if ($item['sub']) {
                        recursiveCategoryTree($item['sub']);
                    }                    
                echo '</li>';
            echo '</ul>';
        }
    }  
?>    
<div class="categories"> 
    <?php recursiveCategoryTree($data['items']); ?>
</div> 

Considering you have multilevel array with 'sub' key for every item if it has children.

As Lajos said you could use partials and even Volt Macros.