Blade Templates
Introduction
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. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade template files use the .blade.php
file extension and are typically stored in the resources/views
directory.
Blade views may be returned from routes or controller using the global view
helper. Of course, as mentioned in the documentation on views, data may be passed to the Blade view using the view
helper's second argument:
Route::get('/', function () {
return view('greeting', ['name' => 'Finn']);
});
Want to take your Blade templates to the next level and build dynamic interfaces with ease? Check out Laravel Livewire.
Displaying Data
You may display data that is passed to your Blade views by wrapping the variable in curly braces. For example, given the following route:
Route::get('/', function () {
return view('welcome', ['name' => 'Samantha']);
});
You may display the contents of the name
variable like so:
Hello, {{ $name }}.
Blade's {{ }}
echo statements are automatically sent through PHP's htmlspecialchars
function to prevent XSS attacks.
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() }}.
HTML Entity Encoding
By default, Blade (and the Laravel e
helper) will double encode HTML entities. If you would like to disable double encoding, call the Blade::withoutDoubleEncoding
method from the boot
method of your AppServiceProvider
:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Blade::withoutDoubleEncoding();
}
}
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, you may use the following syntax:
Hello, {!! $name !!}.
Be very careful when echoing content that is supplied by users of your application. You should typically use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
Blade & JavaScript Frameworks
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 be rendered by your JavaScript framework.
The @
symbol may also be used to escape Blade directives:
{{-- Blade template --}}
@@if()
<!-- HTML output -->
@if()
Rendering 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 Illuminate\Support\Js::from
method directive. The from
method accepts the same arguments as PHP's json_encode
function; however, it will ensure that the resulting JSON is properly escaped for inclusion within HTML quotes. The from
method will return a string JSON.parse
JavaScript statement that will convert the given object or array into a valid JavaScript object:
<script>
var app = {{ Illuminate\Support\Js::from($array) }};
</script>
The latest versions of the Laravel application skeleton include a Js
facade, which provides convenient access to this functionality within your Blade templates:
<script>
var app = {{ Js::from($array) }};
</script>
You should only use the Js::from
method 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.
The @verbatim
Directive
If you are displaying JavaScript variables in a large portion of your template, you may wrap the HTML in the @verbatim
directive so that you do not have to prefix each Blade echo statement with an @
symbol:
@verbatim
<div class="container">
Hello, {{ name }}.
</div>
@endverbatim
Blade Directives
In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.
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 Directives
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