Search code examples
phphtmlsmarty

Php Smarty add class active to link on current page


Is there possibility to add new class to link with class .nav-link on current page in smarty ?

                {foreach FaMenu::gi()->get('top') menu}
                    {if $menu->sub}
                    <li class="list-item dropdown dropdown--relative">
                        <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{$menu->name}
                            <span class="caret"></span></a>
                        <ul class="dropdown-menu dropdown-menu--simple" role="menu">
                            {foreach $menu->sub sub}
                            <li class="dropdown__item"><a class="dropdown__link" href="{$sub->url}" {if $sub->blank}target="_blank"{/if}>{$sub->name}</a></li>
                            {/foreach}
                        </ul>
                    </li>
                    {else}
                    <li class="list-item">
                        <a class="nav-link active" {if $menu->blank}target="_blank"{/if} href="{$menu->url}">{$menu->name} <span class="sr-only">(current)</span></a>
                    </li>
                    {/if}
                {/foreach}

Solution

  • You can get your current location

    {assign var='current_location' value="`$smarty.server.HTTP_HOST``$smarty.server.REQUEST_URI`"}
    

    and than compare it with your menu item url, and if it's match - the item is active