簡體   English   中英

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

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

是否可以在一個Action(或一組Actions)中設置布局無渲染?

據我所知,我可以在config中設置默認布局,它將在每個頁面上呈現。 我可以在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
        ));
    }
}

Zend Expressive中的動作和模板是一對一的,所以我認為應該在適當的模板本身中決定是否應該呈現布局。 基本上,這是省略<?php $this->layout('layout::default'); ?> <?php $this->layout('layout::default'); ?>來自您的操作模板。

在您的特定示例中,您所具有的條件應該導致選擇要呈現的不同模板(不包括布局的模板),而不是向模板發送標志的解決方案。 例如:

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

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

目前我使用普通布局:

<?php

echo $this->content;

PR禁用布局渲染。

現在它可用,只需設置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