Templates

Controller Layouts

One method of using templates in Laravel is via controller layouts. By specifying the layout property on the controller, the view specified will be created for you and will be the assumed response that should be returned from actions.

Defining A Layout On A Controller

  1. class UserController extends BaseController {
  2. /**
  3. * The layout that should be used for responses.
  4. */
  5. protected $layout = 'layouts.master';
  6. /**
  7. * Show the user profile.
  8. */
  9. public function showProfile()
  10. {
  11. $this->layout->content = View::make('user.profile');
  12. }
  13. }

Blade Templating

Blade is a simple, yet powerful templating engine provided with Laravel. Unlike controller layouts, Blade is driven by template inheritance and sections. All Blade templates should use the .blade.php extension.

Defining A Blade Layout

  1. <!-- Stored in app/views/layouts/master.blade.php -->
  2. <html>
  3. <body>
  4. @section('sidebar')
  5. This is the master sidebar.
  6. @show
  7. <div class="container">
  8. @yield('content')
  9. </div>
  10. </body>
  11. </html>

Using A Blade Layout

  1. @extends('layouts.master')
  2. @section('sidebar')
  3. @parent
  4. <p>This is appended to the master sidebar.</p>
  5. @stop
  6. @section('content')
  7. <p>This is my body content.</p>
  8. @stop

Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the @parent directive in a section, allowing you to append to the contents of a layout section such as a sidebar or footer.

Sometimes, such as when you are not sure if a section has been defined, you may wish to pass a default value to the @yield directive. You may pass the default value as the second argument:

  1. @yield('section', 'Default Content')

Other Blade Control Structures

Echoing Data

  1. Hello, {{{ $name }}}.
  2. The current UNIX timestamp is {{{ time() }}}.

Echoing Data After Checking For Existence

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:

  1. {{{ isset($name) ? $name : 'Default' }}}

However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:

  1. {{{ $name or 'Default' }}}

Displaying Raw Text With Curly Braces

If you need to display a string that is wrapped in curly braces, you may escape the Blade behavior by prefixing your text with an @ symbol:

  1. @{{ This will not be processed by Blade }}

Of course, all user supplied data should be escaped or purified. To escape the output, you may use the triple curly brace syntax:

  1. Hello, {{{ $name }}}.

If you don't want the data to be escaped, you may use double curly-braces:

  1. Hello, {{ $name }}.

Note: Be very careful when echoing content that is supplied by users of your application. Always use the triple curly brace syntax to escape any HTML entities in the content.

If Statements

  1. @if (count($records) === 1)
  2. I have one record!
  3. @elseif (count($records) > 1)
  4. I have multiple records!
  5. @else
  6. I don't have any records!
  7. @endif
  8. @unless (Auth::check())
  9. You are not signed in.
  10. @endunless

Loops

  1. @for ($i = 0; $i < 10; $i++)
  2. The current value is {{ $i }}
  3. @endfor
  4. @foreach ($users as $user)
  5. <p>This is user {{ $user->id }}</p>
  6. @endforeach
  7. @forelse($users as $user)
  8. <li>{{ $user->name }}</li>
  9. @empty
  10. <p>No users</p>
  11. @endforelse
  12. @while (true)
  13. <p>I'm looping forever.</p>
  14. @endwhile

Including Sub-Views

  1. @include('view.name')

You may also pass an array of data to the included view:

  1. @include('view.name', array('some'=>'data'))

Overwriting Sections

To overwrite a section entirely, you may use the overwrite statement:

  1. @extends('list.item.container')
  2. @section('list.item.content')
  3. <p>This is an item of type {{ $item->type }}</p>
  4. @overwrite

Displaying Language Lines

  1. @lang('language.line')
  2. @choice('language.line', 1)

Comments

  1. {{-- This comment will not be in the rendered HTML --}}

Extending Blade

Blade even allows you to define your own custom control structures. When a Blade file is compiled, each custom extension is called with the view contents, allowing you to do anything from simple str_replace manipulations to more complex regular expressions.

The Blade compiler comes with the helper methods createMatcher and createPlainMatcher, which generate the expression you need to build your own custom directives.

The createPlainMatcher method is used for directives with no arguments like @endif and @stop, while createMatcher is used for directives with arguments.

The following example creates a @datetime($var) directive which simply calls ->format() on $var:

  1. Blade::extend(function($view, $compiler)
  2. {
  3. $pattern = $compiler->createMatcher('datetime');
  4. return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\'); ?>', $view);
  5. });