This is how you might use a laravel blade directive to render current menu item as active in your blade template without using inline php code directly.
The trick is to get it to render the template at render time NOT compile time so it updates to the current nav item and doesn’t cache.
\htdocs\app\Providers\BladeFormatServiceProvider.php
1 2 3 4 5 |
Blade::directive('setActiveMenuItem', function ($path) { return '<?php if(strpos(\Request::getPathInfo(), "'.$path.'") !== false) echo "item active"; ?>'; }); |
And in your blade template you call it like this:
1 2 3 4 |
<a class="item @setActiveMenuItem(dashboard)" href="{!! url('dashboard') !!}"><i class="home icon"></i>Dashboard</a> <a class="item @setActiveMenuItem(upload)" href="{!! url('upload') !!}"><i class="cloud upload icon"></i>Upload</a> |
That’s it.
