Laravel blade: Can you yield a default partial
Asked Answered
T

5

31

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

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

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

Turpentine answered 3/7, 2014 at 9:15 Comment(1)
Was simple really, although the docs specified a default only as a string you can in fact pass a view @yield('sidebar', \View::make('defaultSidebar'))Turpentine
C
60

Yes you can pass a default

Looking at the documentation

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

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

Ceceliacecil answered 4/7, 2014 at 16:41 Comment(4)
this is not correct. to render a partial you cant just pass a string, instead do as @Turpentine suggested above, doing View::make(pathtoview)Ruthie
I think OP has mentioned that he is aware that he can do it as mentioned on the comment. Was my answer wrong because I stated an example as a string and not a blade?Ceceliacecil
yes because he explicitly said: "I know you can pass a value by default..", and also the question is asking for a partial, not a plain string.Ruthie
problem with this approach is html is escapedAmply
D
20

Most of the time we want multiple line default content, we can use this syntax:

@section('section')
    Default content
@show

For example I have this in the 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 the other view files I can either set the content only:

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

Or I can also set a different layout:

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

The @stop is equivalent as the @endsection.

Donetsk answered 9/9, 2015 at 15:43 Comment(0)
A
17

Although the docs specifies a default only as a string you can in fact pass a view

@yield('sidebar', \View::make('defaultSidebar'))
Anesthesia answered 9/4, 2019 at 23:6 Comment(0)
A
9

Laravel 5.2 added a @hasSection directive that checks if a section is defined in a view. It's not mentioned in 5.3 or 5.4 docs for some reason.

@hasSection('sidebar')
    @yield('sidebar')
@else
    @yield('default-sidebar')
@endif
Anesthesia answered 9/4, 2019 at 23:12 Comment(0)
C
1

Tested in Laravel 8:

@yield can have default content as a second parameter. It can either be a string or a view file

// user-layout.blade.php
@yield('header', View::make('layouts.header'))

You can now override this "header" with section

@section('header')
  <div>New Header</div>
@endsection

//// OR - you can also pass a view file as a second parameter //////

@section('header', View::make('layouts.new-header'))
Corsiglia answered 1/1, 2022 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.