簡體   English   中英

將數據從控制器傳遞到 Laravel 嵌套視圖

[英]Passing data from controller to laravel nested view

我在 Laravel 中有一個頁面系統 - 我將數據從控制器傳遞到視圖。

$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;

現在我通過它如下:

return View::make('Themes.Page', $this->data);

在視圖文件中,我按如下方式訪問數據:

{{$breadcrumb}}

我現在要做的是在嵌套視圖中傳遞這些數據:

$this->layout->nest('content',$page, $this->data);

(內容是視圖中的 {{content}} ,它將被 $page 內容替換。我想像以前一樣傳遞 $this->data 但現在我收到一個錯誤:

未定義變量面包屑。

注意:Laravel 4.2 版 $this->layout 在構造函數中設置為模板文件(Themes.Page)

實際上您不需要將任何單獨的數據傳遞給您的部分頁面(面包屑)

控制器頁面

$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;

return View::make('idea.show',array("data"=>$this->data));

主視圖頁面

<div>
<h1>here you can print data passed from controller  {{$data['title']}}</h1>
@include('partials.breadcrumb')
</div>

你的部分文件

<div>
<h1>here also you can print data passed from controller {{$data['title']}}</h1>
<ul>
<li>....<li>
<li>....<li>
</ul>
</div>

有關這方面的更多信息,您可以查看以下鏈接http://laravel-recipes.com/recipes/90/include-a-blade-template-within-another-template或觀看此視頻https://laracasts.com/series /laravel-5-fundamentals/episodes/13

您應該按如下方式傳遞數據

return View::make('Themes.Page')->with(array(
            'data'=>$this->data));

或(因為您只傳遞 1 個變量)

return View::make('Themes.Page')->with('data', $this->data);

此外,您可以通過引用 $data 將其傳遞給嵌套視圖

$dataForNestedView = ['breadcrumb' => $row->bc];

return View::make('Themes.Page', $this->data)->nest('content', 'page.content', $dataForNestedView);

在 Themes.Page 視圖中呈現嵌套視圖:

<div>
   {{ $content }} <!-- There will be nested view -->
</div> 

在嵌套的 page.content 視圖中,您可以調用:

<div>
  {{ $breadcrumb }}
</div>

*div 標簽只是為了更好地理解。

好吧,經過大量搜索,我發現我使用的 Laravel 4.2 版本中存在一個錯誤。 Laravel 5 有效。 對於 laravel 4.2,更好的選擇是在從控制器傳遞數據時使用View::share('data',$objectarray)傳遞數據對象數組。

謝謝大家的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM