This tutorial aims to guide you through the process of creating reusable layouts and components using Laravel's Blade. By the end of this tutorial, you will have a solid understanding of Blade's template inheritance, sections, and components. You will be able to avoid repetition and save time by reusing common elements across your views.
Blade's template inheritance allows you to define a layout with placeholders for content. These placeholders are defined using @section
and @yield
.
A master layout serves as a template for other views. It contains elements such as the header, navigation bar, footer, etc., that remain constant across different views.
<!-- 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>
In the above code, @yield('title')
is a placeholder for the page title. @section('sidebar')
defines a section that can be filled by child views. @yield('content')
is where the main content of child views will be inserted.
A child view extends a master layout and fills in the @section
placeholders.
<!-- 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
The @extends
directive is used to specify the master layout. @section('title', 'Page Title')
fills the 'title' placeholder.
@section('sidebar')
not only fills the 'sidebar' placeholder but also appends content to the master sidebar using @parent
.
Finally, @section('content')
provides the main content.
Let's create a basic layout with a title placeholder and a content placeholder.
<!-- Stored in resources/views/layouts/basic.blade.php -->
<html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
Here you should expect that the @yield('title')
will be replaced by the page title and @yield('content')
will be replaced by the main content.
Now we'll fill in the placeholders in our basic layout.
<!-- Stored in resources/views/welcome.blade.php -->
@extends('layouts.basic')
@section('title', 'Welcome')
@section('content')
<h1>Welcome to our application!</h1>
@endsection
In this example, 'Welcome' will replace @yield('title')
and the h1
heading will replace @yield('content')
.
In this tutorial, we learned how to create reusable layouts and components using Laravel's Blade. We learned about Blade's template inheritance, how to define a master layout and how to create child views that fill in the placeholders defined by the master layout.
For further learning, explore Blade's other features such as including sub-views, stack directives, and component tags.
Now it's your turn to practice!
Remember, the key to mastering Laravel's Blade is practice and exploration. Happy coding!