Laravel Blade Layouts
A Blade layout is a view that holds the shared HTML for many pages. Each page fills in its own content.
Return a page that uses a layout
The route returns the home view, which uses the layout.
Route::get('/home', fn () => view('home'));The layout
A layout holds the shared HTML, and each page fills in its own content.
<html>
<body>
@yield('content')
</body>
</html>The page
The page names the layout and fills in the content section.
@extends('layout')
@section('content')
<p>Welcome to the library.</p>
@endsectionExample
Route::get('/home', fn () => view('home'));
<html>
<body>
@yield('content')
</body>
</html>
@extends('layout')
@section('content')
<p>Welcome to the library.</p>
@endsectionKey points
- Blade layout: a view that holds the shared HTML for many pages.
- Use @yield('content') to mark the place where a page puts its content.
- Use @extends('layout') in a page to use the layout.
- Use @section('content') to fill in the content for that page.
In this example, the route returns the home view. The home view extends the layout. The layout prints the shared HTML and yields the content. The section fills in the welcome line.
Note
A layout can hold many sections, but one content section suits most pages.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does @yield('content') do in a layout?