jeudi 1 octobre 2015

Check if a section is defined in a view

Using Laravel, how can I modify my layout depending on whether a specific section is defined in a view? I know I can have some default text display if the section does not exist, but imagine the following scenario:

<div class="row">  
     <div class="col-sm-9"> 
         @yield('content')
     </div>
     <div class="col-sm-3">
         @section('sidebar')
         @show
     </div>
</div>

So if my view defines the @section 'sidebar', this works great. But if the view doesn't define a sidebar, I don't want my main content column to be col-sm-9, I want it to be col-sm-12. I was hoping I could do something like this:

@if(@section('sidebar'))
    <div class="row">  
        <div class="col-sm-9"> 
            @yield('content')
        </div>
        <div class="col-sm-3">
            @section('sidebar')
            @show
        </div>
    </div>
@else
    <div class="row">  
        <div class="col-sm-12"> 
            @yield('content')
        </div>
    </div>
@endif

However that doesn't seem to work. Can anyone suggest an alternative?

thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire