简体   繁体   English

将数据从控制器传递到 Laravel 嵌套视图

[英]Passing data from controller to laravel nested view

I have a page system in Laravel - where I pass data from controller to view.我在 Laravel 中有一个页面系统 - 我将数据从控制器传递到视图。

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

Now I passed it as follows:现在我通过它如下:

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

In the view file, I access the data as follows:在视图文件中,我按如下方式访问数据:

{{$breadcrumb}}

What I am trying to do now is to pass this data in nested views:我现在要做的是在嵌套视图中传递这些数据:

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

(Content is the {{content}} in the view which will be replaced with $page contents. I want to pass the $this->data just as before but now I get an error: (内容是视图中的 {{content}} ,它将被 $page 内容替换。我想像以前一样传递 $this->data 但现在我收到一个错误:

Variable breadcrumb not defined.未定义变量面包屑。

Note: Laravel Version 4.2 $this->layout is set in constructor to a template file (Themes.Page)注意:Laravel 4.2 版 $this->layout 在构造函数中设置为模板文件(Themes.Page)

Actually you don't need to pass any separate data to your partial page(breadcrumb)实际上您不需要将任何单独的数据传递给您的部分页面(面包屑)

controller page控制器页面

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

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

main view page主视图页面

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

your partial file你的部分文件

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

for more information on this you can check following links http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template or watch this videohttps://laracasts.com/series/laravel-5-fundamentals/episodes/13有关这方面的更多信息,您可以查看以下链接http://laravel-recipes.com/recipes/90/include-a-blade-template-within-another-template或观看此视频https://laracasts.com/series /laravel-5-fundamentals/episodes/13

You should pass data as follows您应该按如下方式传递数据

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

or (since you're passing only 1 variable)或(因为您只传递 1 个变量)

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

and further you can pass it on to nested views as by referencing $data此外,您可以通过引用 $data 将其传递给嵌套视图

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

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

In the Themes.Page view render nested view:在 Themes.Page 视图中呈现嵌套视图:

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

And in the nested page.content view you can call:在嵌套的 page.content 视图中,您可以调用:

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

*div tag is only for better understanding. *div 标签只是为了更好地理解。

Okay, after intense search, I figured out that there was a bug in the Laravel version 4.2 I was using.好吧,经过大量搜索,我发现我使用的 Laravel 4.2 版本中存在一个错误。 Laravel 5 works. Laravel 5 有效。 For laravel 4.2, a better option would be to pass array of data objects using View::share('data',$objectarray) while passing the data from controller.对于 laravel 4.2,更好的选择是在从控制器传递数据时使用View::share('data',$objectarray)传递数据对象数组。

Thanks everyone for help谢谢大家的帮助

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

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