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

Separate View object

Hello,

In Vokuro, when a new user signs up, a confirmation e-mail is sent to his/her address, using a mail template. The code to get this template is:

return $this->view->getRender('emailTemplates', $name, $parameters, function ($view) {
    $view->setRenderLevel(View::LEVEL_LAYOUT);
});

In the end of the process, the dispatcher forwards the flow to the indexController::indexAction().

In the DI, the View service is declared as shared, so I understand that the above code uses the same View object that will be used later to output the index web page. However the web page does not only render the View::LEVEL_LAYOUT, as set in the code above...

So does the mail template code use a separate View object ? How?

Thanks



2.6k
Accepted
answer

The view object is cloned in the getRender method. The setRenderLevel is applied to the clone instead of the original, keeping the render level for the controller untouched.

public function getRender(string! controllerName, string! actionName, params=null, configCallback=null) -> string
{
    var view;

    /**
     * We must to clone the current view to keep the old state
     */
    let view = clone this;

    ...
}


8.2k

OK, I understand it now. Thank you @waaghals!