2

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

tereško
  • 58,060
  • 25
  • 98
  • 150
Tom
  • 33,626
  • 31
  • 85
  • 109

1 Answers1

7

->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
Tom
  • 33,626
  • 31
  • 85
  • 109
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • Thanks kindly. however I now have the following error. "Argument 3 passed to Illuminate\View\View::nest() must be of the type array, object given. Called in routes.php" – Tom Jul 09 '13 at 14:40
  • I've edited it to pass an array as the third argument rather than an object, which should resolve that error – fideloper Jul 09 '13 at 15:49
  • 1
    Actually that edit needs peer-review. You can change `$users` to `array('users' => $users)` in that third parameter of the `nest()` method: `return $layout->nest('content','list-template', array('users' => $users));` – fideloper Jul 09 '13 at 15:57