简体   繁体   中英

Layout no render on some actions in Zend Expressive?

Is it possible to set layout no render in one Action (or set of Actions)?

As I know I can set default layout in config, which will render on every page. I can change it in Action bay passing the 'layout' variable with value, but is it possible to not render layout at all?

class IndexAction
{
    private $template;

    public function __construct(Template $template){ ... }

    public function __invoke($request, $response, $next = null)
    {
        if(!$request->hasHeader('X-Requested-With')){
            $data = ['layout' => 'new\layout']; //change default layout to new one
        }
        else{
            $data = ['layout' => false]; //I need only to return view ?
        }

        return new HtmlResponse($this->template->render(
            'web::index', $data
        ));
    }
}

Actions and templates in Zend Expressive are one-to-one, so I think that the decision whether layout should be rendered or not should be made in the appropriate template itself. Basically, it's a matter of omitting <?php $this->layout('layout::default'); ?> <?php $this->layout('layout::default'); ?> from your action template.

In your particular example, that condition you have should result in choosing different template to be rendered (one that doesn't include layout), instead of a solution with sending a flag to the template. For example:

$templateName = $request->hasHeader('X-Requested-With') 
    ? 'template-without-layout' 
    : 'template-with-layout';

return new HtmlResponse($this->template->render($templateName));

Currently I use plain layout:

<?php

echo $this->content;

There is PR to disable layout rendering.

Now it's available, just set layout = false

    return new HtmlResponse($this->template->render(
        'web::index', 
        ['layout' => false]
    ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM