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.

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



1.5k
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;

    ...
}


5.0k

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