Laravel blade: can you get a partial

If I have a layout called RightSideBar.blade.php in the RightSideBar.blade.php Laravel blade , one yield('content') yield('content') and another yield('sidebar') .

Is there a built-in way to display default partial if the view extending RightSideBar does not have section('sidebar') ?

I know that you can pass the default value , just wondering if there is a way to make the default partial.

+19
php laravel blade laravel-blade
source share
4 answers

Yes, you can pass the default value

Looking through the documentation

@yield('sidebar', 'Default Content');

Which basically puts the default output when the child template does not have @section('sidebar')

+30
source share

In most cases, we want to have multiple lines by default, we can use this syntax:

 @section('section') Default content @show 

For example, I have this in a template file:

 @section('customlayout') <article class="content"> @yield('content') </article> @show 

You can see the difference between @show and @ stop / @ endsection: the above code is equivalent to the one below:

 @section('customlayout') <article class="content"> @yield('content') </article> @stop @yield('customlayout') 

In other view files, I can either install only the content:

 @section('content') <p>Welcome</p> @stop 

Or I can also set another layout:

 @section('content') <p>Welcome</p> @stop @section('defaultlayout') <div> @yield('content') </div> @stop 

@stop is equivalent to @endsection.

+12
source share

Although the docs only show the default value as a string, in fact you can pass the view

 @yield('sidebar', \View::make('defaultSidebar')) 
+4
source share

In Laravel 5.2, the @hasSection directive is @hasSection which checks if a section is defined in a view. This is not mentioned in 5.3 or 5.4 documents for some reason.

 @hasSection('sidebar') @yield('sidebar') @else @yield('default-sidebar') @endif 
+1
source share

All Articles