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.

Namespace Model access in volt

I think not yet @baychae but you can inject a component in DI and then use it on volt

Can you do that in Twig? Other way use Twig

Good luck



1.4k
Accepted
answer

You should be doing all your logic inside the controller. The view should only be used to output the data returned from your controller.

class Controller {
    public function viewAction()  {
        $this->view->records = Model::find();
    }
}

and in your view

{% foreach record in records %}
    {{ record.id }}
{% endforeach %}

You will find it to be really hard to update your code if you start doing everything inside the view. Especially when you start splitting your views up into components.



25.2k

I did indeed end up doing this.

Using model fetch in the view can easily lead to a poorly performing view without discipline.

I increased performance by moving business logic to the Model and fetch and transform to the Controller.

>You should be doing all your logic inside the controller. The view should only be used to output the data returned from your controller. > >``` >class Controller { > public function viewAction() { > $this->view->records = Model::find(); > } >} >``` > >and in your view > >``` >{% foreach record in records %} > {{ record.id }} >{% endforeach %} >``` > >You will find it to be really hard to update your code if you start doing everything inside the view. Especially when you start splitting your views up into components.