Blade Templates
Introduction
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, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php
file extension and are typically stored in the resources/views
directory.
Template Inheritance
Defining A Layout
Two of the primary benefits of using Blade are template inheritance and sections. To get started, let's take a look at a simple example. First, we will examine a "master" page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:
<!-- Stored in resources/views/layouts/app.blade.php -->
<html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
As you can see, this file contains typical HTML mark-up. However, take note of the @section
and @yield
directives. The @section
directive, as the name implies, defines a section of content, while the @yield
directive is used to display the contents of a given section.
Now that we have defined a layout for our application, let's define a child page that inherits the layout.
Extending A Layout
When defining a child view, use the Blade @extends
directive to specify which layout the child view should "inherit". Views which extend a Blade layout may inject content into the layout's sections using @section
directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield
:
<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.app')
@section('title', 'Page Title')
@section('sidebar')
@@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection
In this example, the sidebar
section is utilizing the @@parent
directive to append (rather than overwriting) content to the layout's sidebar. The @@parent
directive will be replaced by the content of the layout when the view is rendered.
Contrary to the previous example, this sidebar
section ends with @endsection
instead of @show
. The @endsection
directive will only define a section while @show
will define and immediately yield the section.
The @yield
directive also accepts a default value as its second parameter. This value will be rendered if the section being yielded is undefined:
@yield('content', View::make('view.name'))
Blade views may be returned from routes using the global view
helper:
Route::get('blade', function () {
return view('child');
});
Components & Slots
Components and slots provide similar benefits to sections and layouts; however, some may find the mental model of components and slots easier to understand. First, let's imagine a reusable "alert" component we would like to reuse throughout our application:
<!-- /resources/views/alert.blade.php -->
<div class="alert alert-danger">
{{ $slot }}
</div>
The {{ $slot }}
variable will contain the content we wish to inject into the component. Now, to construct this component, we can use the @component
Blade directive:
@component('alert')
<strong>Whoops!</strong> Something went wrong!
@endcomponent
To instruct Laravel to load the first view that exists from a given array of possible views for the component, you may use the componentFirst
directive:
@componentFirst(['custom.alert', 'alert'])
<strong>Whoops!</strong> Something went wrong!
@endcomponent
Sometimes it is helpful to define multiple slots for a component. Let's modify our alert component to allow for the injection of a "title". Named slots may be displayed by "echoing" the variable that matches their name:
<!-- /resources/views/alert.blade.php -->
<div class="alert alert-danger">
<div class="alert-title">{{ $title }}</div>
{{ $slot }}
</div>
Now, we can inject content into the named slot using the @slot
directive. Any content not within a @slot
directive will be passed to the component in the $slot
variable:
@component('alert')
@slot('title')
Forbidden
@endslot
You are not allowed to access this resource!
@endcomponent
Passing Additional Data To Components
Sometimes you may need to pass additional data to a component. For this reason, you can pass an array of data as the second argument to the @component
directive. All of the data will be made available to the component template as variables:
@component('alert', ['foo' => 'bar'])
...
@endcomponent
Aliasing Components
If your Blade components are stored in a sub-directory, you may wish to alias them for easier access. For example, imagine a Blade component that is stored at resources/views/components/alert.blade.php
. You may use the component
method to alias the component from components.alert
to alert
. Typically, this should be done in the boot
method of your AppServiceProvider
:
use Illuminate\Support\Facades\Blade;
Blade::component('components.alert', 'alert');