28

I'm trying to get my default template working with Laravel. I'm coming from Codeigniter and Phil Sturgeon's template system so I'm trying to do it in a similar way. Can anyone help me with what I'm missing/doing wrong? Thanks!

//default.blade.php (located in layouts/default)
<html>
    <title>{{$title}}</title>
    <body>
    {{$content}}
    </body>
</html>
//end default.blade.php

//home.blade.php (index view including header and footer partials)
@layout('layouts.default')
@include('partials.header')
//code
@include('partials.footer')
//end home

//routes.php (mapping route to home controller)
Route::controller( 'home' );
//end

//home.php (controller)
<?php
class Home_Controller extends Base_Controller {
    public $layout = 'layouts.default';
    public function action_index()
    {   
        $this->layout->title = 'title';
        $this->layout->content = View::make( 'home' );
    }
}
//end
Peter Kota
  • 8,048
  • 5
  • 25
  • 51
coryj
  • 1,255
  • 3
  • 14
  • 29
  • 1
    What's the problem you're facing? At least tell us the error message :) – akhy Sep 21 '12 at 07:09
  • Laravel on Stackexchange http://area51.stackexchange.com/proposals/46607/laravel?referrer=VUgOWgZpXyO753uZWv1VMg2 – Brian Dillingham Apr 08 '13 at 21:54
  • I know your problem is solved, but I recommend you using the section in laravel blade rather then assigning view in controller. Its simple yet fully blade way to achieve the same.. – Daksh Mehta Jan 12 '15 at 14:29

2 Answers2

86

You are mixing two different layout approaches of Laravel. This way you are rendering the layout view, include the home view and try to include inside again the layout.

My personal preference is the controller approach.

Controller Layouts

The controller and the layouts can remain the same.

Note: As a shortcut you could nest the content instead of View::make, that automaically renders it when you echo it out in the layout.

In home.blade.php remove the @layout function.

Edit (example):

controllers/home.php

<?php
class Home_Controller extends Base_Controller {
  public $layout = 'layouts.default';
  public function action_index()
  {
    $this->layout->title = 'title';
    $this->layout->nest('content', 'home', array(
      'data' => $some_data
    ));
  }
}

views/layouts/default.blade.php

<html>
  <title>{{ $title }}</title>
  <body>
    {{ $content }}
  </body>
</html>

views/home.blade.php

Partials are included in the content.

@include('partials.header')
{{ $data }}
@include('partials.footer')

Blade Layouts

If you want this approach you have a few problems there. First, you are including new content after the layout. Not sure if intentional, but the @layout function itself is basicly just an @include restricted to be at the very beginning of the view. So if your layout is a closed html, any include after that will be appended after your html layout.

Your content should use sections here with the @section function and @yield it in your layout. The header and footer could be included in the layout with @include or if you want to define it in the content view then put those in a @section too, like below. If you define it that way if a section doesn't exist nothing gets yielded.

controllers/home.php

<?php
class Home_Controller extends Base_Controller {
  public function action_index()
  {
    return View::make('home')->with('title', 'title');
  }
}

views/layouts/default.blade.php

<html>
 <title>{{$title}}</title>
 <body>
  @yield('header')
  @yield('content')
  @yield('footer')
 </body>
</html>

views/home.blade.php

@layout('layouts.default')
@section('header')
  header here or @include it
@endsection
@section('footer')
  footer
@endsection
@section('content')
  content
@endsection
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
TLGreg
  • 8,321
  • 3
  • 23
  • 12
  • Can you show me more of the controller approach? This is the method that I was going for but my variables we're not being passed. (Use of undefined constant title - assumed 'title'). I used $this->layout->nest('content', 'home'); and echoed {{'content'}} and {{'title'}} in my default layout. Removed @layout function as well, still can't figure this out. – coryj Sep 21 '12 at 12:53
  • Not sure what causes your problem, you can bind a variable to the layout just like you did, it should work, if not some other code could be the cause. If you want to assign a variable to the nested view you can do it in a third parameter of the *nest()* method. Like so: *$this->layout->nest('content', 'home', array('data', $data));* But I add the examples for that too. If it's not working add some more details in your post. – TLGreg Sep 21 '12 at 13:26
  • Thanks, your controller example helped me a lot! – coryj Sep 22 '12 at 15:04
  • 1
    +1 for the controller layout approach, never thought of that before. Where did you get this idea? I think I never found it in the official docs. – akhy Nov 18 '12 at 10:07
  • Thanks for showing me how nest works. i couldn't figure out that the layout need to use a variable instead of a yield as the location where the nested view was passed in. – Eddie Monge Jr Jan 12 '13 at 23:13
  • A caveat for those who opt for the Blade approach: `@layout()` seems to have been replace by `@extends()` in Laravel 4. – Tomas Buteler Dec 07 '13 at 19:38
  • Oh yeah, the post was still made for L3. Also `@endsection` is deprecated in L4, you should start using `@stop` instead. And ofcourse everything should be PSR-1 in L4. – TLGreg Dec 11 '13 at 01:53
  • thats's ,+1 really a nice stuff – Rakesh Sharma Dec 12 '14 at 09:00
  • In my case open a blank page..after flow your code – Priyanka Ahire May 11 '16 at 05:58
  • May be home can not be call – Priyanka Ahire May 11 '16 at 06:16
  • for info on how to correctly include views see https://stackoverflow.com/questions/21753954/how-to-include-a-sub-view-in-blade-templates/21755728#21755728 – GWed Nov 15 '17 at 16:09
0

The answer given above explains how templating is done in Laravel, however to gain additional benefits like managing themes organised into a theme directory with ability to switch between themes and having partials and theme resources all together sounds like almost something similar to Phil Sturgeon Template Library for CI. You may want to check the Theme bundle for Laravel. Here is the link:

http://raftalks.github.io/Laravel_Theme_Bundle/

aug
  • 11,138
  • 9
  • 72
  • 93
Raftalks
  • 2,017
  • 1
  • 21
  • 25