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

Is it possible to render a controller via Volt

I want to display a list of users in a sidebar menu and am looking for a solution to render a controller/result in the main template without having to set a variable in the controllerbase. What I'm looking for would be similar to this in symfony:

{{ render(controller('AcmeUserBundle:User:userList') }}

This is very bad idea and shouldn't be done. Even in symfony it's recommended to be avoided to call other action/controller from view. You should just pass parameters from controller action with your users to your view and use {% include %} for example.



338
Accepted
answer

Possible solution is to create a component:

use Phalcon\Mvc\User\Component,
    Phalcon\Mvc\View;

/**
 * @property View $view
 */
class Fragment extends Component
{
    public function showData()
    {
        $viewData['items'] = ['your', 'data', 'here'];
        return $this->view->getRender('fragment', 'show-data', $viewData);
    }
}
$di->set('fragment', function() {
    return new Fragment();
});

<viewsDir>/fragment/show-data.volt:

{% for item in items %}
    <p>{{ item }}</p>
{% endfor %}

Usage:

{{ fragment.showData() }}