简体   繁体   English

Zend Expressive中的某些操作的布局没有渲染?

[英]Layout no render on some actions in Zend Expressive?

Is it possible to set layout no render in one Action (or set of Actions)? 是否可以在一个Action(或一组Actions)中设置布局无渲染?

As I know I can set default layout in config, which will render on every page. 据我所知,我可以在config中设置默认布局,它将在每个页面上呈现。 I can change it in Action bay passing the 'layout' variable with value, but is it possible to not render layout at all? 我可以在Action中更改它,并使用值传递'layout'变量,但是有可能根本不渲染布局吗?

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. Zend Expressive中的动作和模板是一对一的,所以我认为应该在适当的模板本身中决定是否应该呈现布局。 Basically, it's a matter of omitting <?php $this->layout('layout::default'); ?> 基本上,这是省略<?php $this->layout('layout::default'); ?> <?php $this->layout('layout::default'); ?> from your action template. <?php $this->layout('layout::default'); ?>来自您的操作模板。

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. PR禁用布局渲染。

Now it's available, just set layout = false 现在它可用,只需设置layout = false

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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