Templating in Laravel
Asked Answered
B

2

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
Birdman answered 21/9, 2012 at 5:26 Comment(3)
What's the problem you're facing? At least tell us the error message :)Ginder
Laravel on Stackexchange area51.stackexchange.com/proposals/46607/…Crochet
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..Baillie
F
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
Felly answered 21/9, 2012 at 9:16 Comment(11)
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.Birdman
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.Felly
Thanks, your controller example helped me a lot!Birdman
+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.Ginder
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.Yard
A caveat for those who opt for the Blade approach: @layout() seems to have been replace by @extends() in Laravel 4.Tiki
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.Felly
thats's ,+1 really a nice stuffChigoe
In my case open a blank page..after flow your codeSubarctic
May be home can not be callSubarctic
for info on how to correctly include views see #21754454Analogical
I
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/

Incorruptible answered 27/9, 2012 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.