Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 785
Lannar Avatar asked Oct 01 '15 13:10

Lannar


People also ask

How do you know if a element is in a viewport?

Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.

How do I see if a div is visible on screen?

You can use .is(':visible') selects all elements that are visible.

Is element in viewport jquery?

Check if element is visible in viewport using jquery: If the bottom position of the viewport is greater than the element's top position AND the top position of the viewport is less than the element's bottom position, the element is in the viewport (at least partially).

How do you check if element is visible after scrolling react?

To know whether the element is fully visible in viewport, you will need to check whether top >= 0, and bottom is less than the screen height. In a similar way you can also check for partial visibility, top is less than screen height and bottom >= 0. The Javascript code could be written as : window.


2 Answers

You can use this to tell if a section is defined or not :

@if (!empty($__env->yieldContent('sidebar')))
   //It is defined
@endif

$__env->yieldContent() returns the content of the specified section, or an empty string if it is not defined or empty.

Edit

Prior to PHP 5.5 empty() will not work on the result of a function directly, so you can use trim() instead, like so :

@if (trim($__env->yieldContent('sidebar')))
   //It is defined
@endif
like image 117
Drown Avatar answered Oct 08 '22 18:10

Drown


Laravel 5.2 added a @hasSection directive that does exactly what you're asking. It's not mentioned in 5.3 or 5.4 docs for some reason.

@hasSection('title')
    <title>@yield('title') - {{ config('app.name') }}</title>
@else
    <title>{{ config('app.name') }}</title>
@endif
like image 35
blade Avatar answered Oct 08 '22 19:10

blade