Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Blade @ifSection() tag
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class BladeServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('ifSection');
$replace = '$1<?php if (isset($__env->getSections()[$2])): ?>';
return preg_replace($pattern, $replace, $view);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
@ifSection('breadcrumbs')
<div class="breadcrumbs">
@yield('breadcrumbs')
</div>
@else
(Optional default value)
@endif
@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

davejamesmiller commented May 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.