2016-09-30 36 views
0

是否可以在一個Action(或一組Actions)中設置佈局不渲染?Zend表達式中的某些操作沒有渲染布局?

據我所知,我可以在配置中設置默認佈局, 這將在每個頁面上呈現。 我可以改變它在行動灣傳遞'佈局'變量的價值, 但它可能根本不渲染布局?

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 
     )); 
    } 
} 
+0

如果你試圖返回JSON數據也有個'Zend的\ Diactoros \響應\ JsonResponse'。你可以這樣使用它:'return new JsonResponse($ dataArray);' – xtreamwayz

+0

謝謝!但我需要返回HTML。在第一個請求(無ajax)中需要返回Layout + View,並且如果它是ajax請求只返回View,因爲我不想再次加載佈局。 – tasmaniski

回答

0

現在是可用的,只要設置佈局=虛假

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

操作和模板在Zend的表現是一個對一個,所以我認爲,佈局是否應該呈現與否的決定應在適當的模板本身進行。基本上,這是從您的操作模板中省略<?php $this->layout('layout::default'); ?>的問題。

在您的特定示例中,您所擁有的條件應導致選擇不同的模板進行渲染(不包括佈局),而不是向模板發送標誌的解決方案。例如:

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

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

目前我使用簡單的佈局:

<?php 

echo $this->content; 

PR禁用佈局呈現。