CodeIgniter: How to 'highlight' the link of the page the user is currently on?

前端 未结 5 579
渐次进展
渐次进展 2021-01-02 21:12

Fairly new to CodeIgniter, still grasping the MVC approach. I\'m just wondering what\'s the best way to solve this:

I got my navigation bar highlighting the currentl

5条回答
  •  青春惊慌失措
    2021-01-02 21:45

    I used @Calle example, worked really well... I did have to use a custom url grabber instead of the CI current_url() as I'm bypassing the index.php file with .htaccess. I have also added the correct attribute tag.

    Notes for beginners I crated this file in 'helpers' called 'menu_helper.php' and loaded it via the controller $this->load->helper('menu');

    helpers/menu_helper.php

    if ( ! function_exists('menu_anchor'))
    {
        function menu_anchor($uri = '', $title = '', $attributes = '')
        {
            $title = (string) $title;
    
            if ( ! is_array($uri))
            {
                $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
            }
            else
            {
                $site_url = site_url($uri);
            }
    
            if ($title == '')
            {
                $title = $site_url;
            }
    
            if ($attributes != '')
            {
                $attributes = _parse_attributes($attributes);
            }
    
            $current_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
            $attributes .= ($site_url == $current_url) ? 'class="active"' : 'class="normal"';
    
    
            return ''.$title.'';
        }
    }
    

    The view file:

    The css:

    ul.topnav li.topnav-1 a.active { something clever }

提交回复
热议问题