To include a Blade template within another Blade template in Laravel, you can use the @include
directive.
Create the Blade template you want to include. For example, let's say you have a file called header.blade.php
that contains the header HTML for your website.
In the Blade template where you want to include the header.blade.php
file, use the @include
directive followed by the path to the file. For example, if your file is located in the resources/views/includes
directory, you would use:
@include('includes.header')
Save the file and when the template is rendered, the contents of the header.blade.php
file will be included at that location.
You can also pass data to the included template by adding a second parameter to the @include
directive, like this:
@include('includes.header', ['title' => 'My Website'])
Then, in the included template (header.blade.php
), you can access the data using the variable names specified in the included template:
<title>{{ $title }}</title>
This allows you to pass dynamic data to the included template.