Skip to content

WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 12.x.

Templates

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 resources/views/layouts/master.blade.php -->
2 
3<html>
4 <head>
5 <title>App Name - @yield('title')</title>
6 </head>
7 <body>
8 @section('sidebar')
9 This is the master sidebar.
10 @show
11 
12 <div class="container">
13 @yield('content')
14 </div>
15 </body>
16</html>

Using A Blade Layout

1@extends('layouts.master')
2 
3@section('title', 'Page Title')
4 
5@section('sidebar')
6 @@parent
7 
8 <p>This is appended to the master sidebar.</p>
9@stop
10 
11@section('content')
12 <p>This is my body content.</p>
13@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

1Hello, {{ $name }}.
2 
3The 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 }}

If you don't want the data to be escaped, you may use the following syntax:

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

Be very careful when echoing content that is supplied by users of your application. Always use the double 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 
9@unless (Auth::check())
10 You are not signed in.
11@endunless

Loops

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

Including Sub-Views

1@include('view.name')

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

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

Overwriting Sections

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

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

Displaying Language Lines

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

Comments

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

Laravel is the most productive way to
build, deploy, and monitor software.