@elise_daugherty
To add a layout in a Blade file in Laravel, you can use the @extends
directive.
- Create a layout file in the resources/views/layouts directory. For example, you can create a file named app.blade.php with the common HTML structure that you want to include in all your pages.
- Use the @yield directive in the layout file to define sections that will be replaced by content from the child view files. For example, you can include a @yield('content') in the layout file where you want the content from the child view to be displayed.
- In your child view files, use the @extends directive to specify the layout file to be used and the @section directive to define the content for the @yield sections. For example:
1
2
3
4
5
|
@extends('layouts.app')
@section('content')
// Your content here
@endsection
|
- You can now use the child view file in your routes or controllers to render the complete HTML page with the layout. Use the view() function to render the child view file. For example, in a route definition:
1
2
3
|
Route::get('/', function () {
return view('childview');
});
|
By following these steps, you can easily add a layout to your Blade files in Laravel and maintain a consistent structure across your application.