template-enginezend-viewmezzio

Zend Expressive: how to change layout using Zend View


Zend Expressive defaults to layout template when using Zend View. I note the addTemplate($template) function in PhpRenderer class but where and how to add an alternative template to layout?

In a middleware factory of an action, in the action itself, or somewhere else?


Solution

  • Passing layout key to render() method of the renderer in data array seems like enough to switch layout just before returning the response.

    For example:

    class HomeAction
    {
       public function __invoke($request, $response, $next)
       {
         $data = [
            'layout' => 'layout::default',
            // or 'layout::admin',
            // or 'layout::alternative',
         ];
    
         $body = $this->template->render('app::home', $data);
    
         return new HtmlResponse($body);
       }
    }
    

    I strongly recommend watching of repository and it's issue updates on github.

    See #314 and #317.