Check if a section is defined in a view Check if a section is defined in a view laravel laravel

Check if a section is defined in a view


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


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