Add view within another view in Blade Laravel
view-js.blade.php which wraps the JS code within
<script> tag and a CSS blade file called
view-css.blade.php which wraps the CSS code within
<style> tag. And all you want to do is add these to an another blade file called
This is where Blade’s
@include directive can come to your rescue.
@include directive do is allow you to include a Blade view from within another view. So, for our example if we want to achieve the above mentioned scenario, we could do like so.
We would just need to add the following into
@include('view-css') @include('view-js') <!-- Some more html -->
It’s as easy as that! Notice here that all the variables that are available to the parent view will be made available to the included view as well.
Apart from this, you can also pass additional data (in form of an array) to these views altogether like so.
@include('view-js', ['path' => '/'])
In order to prevent Laravel from throwing error if the related Blade view is not found, you could use
@includeIf which will onlu include the view if it actually exist.
When you only want to add the Blade view when a certain boolean condition matched, you could use
@includeWhen directive like so.
Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!