Created
December 8, 2016 10:16
-
-
Save fer-ri/4fea11f94394876bd2dc59710df8510e to your computer and use it in GitHub Desktop.
Laravel Blade Directive To Set Active Menu Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Blade::directive('active', function ($expression) { | |
return "<?php echo active_url($expression); ?>"; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (! function_exists('active_url')) { | |
function active_url($pattern, $default = 'active') | |
{ | |
$path = request()->decodedPath(); | |
if (@preg_match('#^'.$pattern.'\z#u', $path)) { | |
return $default; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks ! great job