Note that Blade is fully documented through Laravel and we would encourage you to check out their official docs, but we will be presenting pieces of documentation here to help newcomers get started quickly.
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. In fact, all Blade views are compiled into plain PHP code and cached until they are modified. Blade view files use the .blade.php file
extension and are stored in the resources/views
directory.
Data Display Syntax
You may display data passed to your Blade views by wrapping the variable in curly braces to echo the value. For example, given the following variable:
$name = 'John Doe';
You may display the contents of the name variable like so:
Hello, {{ $name }}.
Note that Blade
{{ }}
statements are automatically sent through PHP'shtmlspecialchars
function to prevent XSS attacks. This means any code markup will not be rendered on the page but displayed as plain text.
You are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:
The current UNIX timestamp is {{ time() }}.
Displaying Unescaped Data
By default, Blade {{ }}
statements are automatically sent through PHP's htmlspecialchars
function to prevent XSS attacks. If you do not want your data to be escaped and would like to render the code markup it contains, you may use the following syntax:
Hello, {!! $name !!}.
Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
Display data if exists
Some logic can be expressed through Blade {{ }}
:
// Ternary operator
{{ $name ? $name : 'default' }}
// Display variable value if isset
{{ isset($name) ? $name : 'Default' }}
// Isset ternary operator shortcut
{{ $name ?? 'Default' }}
Displaying data for JavaScript
Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the @
symbol to inform the Blade rendering engine an expression should remain untouched. For example:
<h1>Laravel</h1>
Hello, @{{ name }}.
In this example, the @
symbol will be removed by Blade; however, {{ name }}
expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.
Comments
Blade also allows you to define comments in your views. However, unlike HTML comments, Blade comments are not included in the HTML returned by your application:
{{-- This comment will not be present in the rendered HTML --}}
Directive Syntax
Directives are a special Blade syntax. They're a way to harness the power of PHP functions directly in your views without having to explicitly write out PHP. Directives are marked with an @
before them and may be used as single line functions or have a closure.
Here we'll cover some common directives you'll need to have under your belt for successful theme development. Please visit our Directives reference page for a full list of available directives.
Conditionals
If Statements
You may construct if
statements using the @if
, @elseif
, @else
, and @endif
directives. These directives function identically to their PHP counterparts:
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
For convenience, Blade also provides an @unless
directive:
@unless (Auth::check())
You are not signed in.
@endunless
In addition to the conditional directives already discussed, the @isset
and @empty
directives may be used as convenient shortcuts for their respective PHP functions:
@isset($records)
// $records is defined and is not null...
@endisset
@empty($records)
// $records is "empty"...
@endempty
Authentication Conditionals
The @auth
and @guest
directives may be used to quickly determine if the current user is authenticated or is a guest:
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
If needed, you may specify the authentication guard that should be checked when using the @auth
and @guest
directives:
@auth('admin')
// The user is authenticated...
@endauth
@guest('admin')
// The user is not authenticated...
@endguest
Section Conditional
You may check if a section has content using the @hasSection
directive:
@hasSection('navigation')
<div class="pull-right">
@yield('navigation')
</div>
<div class="clearfix"></div>
@endif
Environment Conditional
You may check if the application is running in the production environment using the @production
directive:
@production
// Production specific content...
@endproduction
Or, you may determine if the application is running in a specific environment using the @env
directive:
@env('staging')
// Staging specific content...
@endenv
Switch Statements
Switch statements can be constructed using the @switch
, @case
, @break
, @default
and @endswitch
directives:
@switch($i)
@case(1)
First case...
@break
@case(2)
Second case...
@break
@default
Default case...
@endswitch
Loops
In addition to conditional statements, Blade provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
@while (true)
<p>I'm looping forever.</p>
@endwhile
When looping, you may use the loop variable to gain valuable information about the loop, such as whether you are in the first or last iteration through the loop.
When using loops you may also end the loop or skip the current iteration:
@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
@endforeach
You may also include the condition with the directive declaration in one line:
@foreach ($users as $user)
@continue($user->type == 1)
<li>{{ $user->name }}</li>
@break($user->number == 5)
@endforeach
The Loop Variable
When looping, a $loop
variable will be available inside of your loop. This variable provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop:
@foreach ($users as $user)
@if ($loop->first)
This is the first iteration.
@endif
@if ($loop->last)
This is the last iteration.
@endif
<p>This is user {{ $user->id }}</p>
@endforeach
If you are in a nested loop, you may access the parent loop's $loop
variable via the parent
property:
@foreach ($users as $user)
@foreach ($user->posts as $post)
@if ($loop->parent->first)
This is first iteration of the parent loop.
@endif
@endforeach
@endforeach
The $loop
variable also contains a variety of other useful properties:
Property | Description |
---|---|
$loop->index | The index of the current loop iteration (starts at 0). |
$loop->iteration | The current loop iteration (starts at 1). |
$loop->remaining | The iterations remaining in the loop. |
$loop->count | The total number of items in the array being iterated. |
$loop->first | Whether this is the first iteration through the loop. |
$loop->last | Whether this is the last iteration through the loop. |
$loop->even | Whether this is an even iteration through the loop. |
$loop->odd | Whether this is an odd iteration through the loop. |
$loop->depth | The nesting level of the current loop. |
$loop->parent | When in a nested loop, the parent's loop variable. |
PHP
In some situations, it's useful to embed PHP code into your views. You can use the Blade @php
directive to execute a block of plain PHP within your template:
@php
//
@endphp
While Blade provides this feature, using it frequently may be a signal that you have too much logic embedded within your template.
JSON
Sometimes you may pass an array to your view with the intention of rendering it as JSON in order to initialize a JavaScript variable. For example:
<script>
var app = <?php echo json_encode($array); ?>;
</script>
However, instead of manually calling json_encode
, you may use the @json
Blade directive. The @json
directive accepts the same arguments as PHP's json_encode
function:
<script>
var app = @json($array);
var app = @json($array, JSON_PRETTY_PRINT);
</script>
You should only use the
@json
directive to render existing variables as JSON. The Blade templating is based on regular expressions and attempts to pass a complex expression to the directive may cause unexpected failures.
Stacks
Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout. This can be particularly useful for specifying any JavaScript libraries required by your views.
@stack
To render the complete stack contents, pass the name of the stack to the @stack
directive.
<head>
<!-- Head Contents -->
@stack('scripts')
</head>
@push
To push content to a stack, utilize the @push
directive. You may push to a stack as many times as needed.
@push('scripts')
<script src="/example.js"></script>
@endpush
@prepend
If you would like to prepend content onto the beginning of a stack, you should use the @prepend
directive:
@push('scripts')
This will be second...
@endpush
// Later...
@prepend('scripts')
This will be first...
@endprepend