Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



331
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() }}