Laravel 4: Nest view inside layout with data

回眸只為那壹抹淺笑 提交于 2019-12-21 21:36:43

问题


I'm writing a simple app which only relies on a few routes and views. I've setup an overall layout and successfully nested a template using the following.

routes.php

View::name('layouts.master', 'master');
$layout = View::of('master');

Route::get('/users', function() use ($layout)
{
    $users = Users::all()
    return $layout->nest('content','list-template');
});

master.blade.php

<h1>Template</h1>
<?=$content?>

list-template.php

foreach($users as $user) {
   echo $user->title;
}

How do I pass the query results $users into my master template and then into list-temple.php?

Thanks


回答1:


->nest allows a 3rd argument for an array of data:

   Route::get('/users', function() use ($layout)
    {
        $users = Users::all()
        return $layout->nest('content','list-template', array('users' => $users));
    });

Also in your master.blade.php file - change it to this:

<h1>Template</h1>
@yield('content')

list-template.blade.php <- note the blade filename:

@extends('layouts.master')

@section('content')
<?php
  foreach($users as $user) {
     echo $user->title;
   }
?>
@stop


来源:https://stackoverflow.com/questions/17550562/laravel-4-nest-view-inside-layout-with-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!