Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templating in Laravel

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 
like image 700
coryj Avatar asked Sep 21 '12 05:09

coryj


People also ask

What is templating in Laravel?

The Blade is a powerful templating engine in a Laravel framework. The blade allows to use the templating engine easily, and it makes the syntax writing very simple. The blade templating engine provides its own structure such as conditional statements and loops.

What is the templating engine used in Laravel?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

What is a blade file?

Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views.


1 Answers

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 
like image 124
TLGreg Avatar answered Sep 20 '22 00:09

TLGreg