Skip to content

Instantly share code, notes, and snippets.

@fer-ri
Created December 8, 2016 10:16
Show Gist options
  • Save fer-ri/4fea11f94394876bd2dc59710df8510e to your computer and use it in GitHub Desktop.
Save fer-ri/4fea11f94394876bd2dc59710df8510e to your computer and use it in GitHub Desktop.
Laravel Blade Directive To Set Active Menu Class
<?php
Blade::directive('active', function ($expression) {
return "<?php echo active_url($expression); ?>";
});
<?php
if (! function_exists('active_url')) {
function active_url($pattern, $default = 'active')
{
$path = request()->decodedPath();
if (@preg_match('#^'.$pattern.'\z#u', $path)) {
return $default;
}
}
}
<ul class="nav nav-pills">
<li role="presentation" class="@active('foods/?.*')"><a href="#">Foods</a></li>
<li role="presentation" class="@active('glossary/?.*')"><a href="#">Glossary</a></li>
<li role="presentation" class="@active('recipes/?.*')"><a href="#">Recipes</a></li>
<li role="presentation" class="@active('tools/?.*')"><a href="{{ url('/tools') }}">Tools</a></li>
</ul>
@olriko
Copy link

olriko commented Feb 28, 2017

Thanks ! great job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment