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
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:
- =menu_anchor(base_url()."path/to/something", "menu_item")?>
- =menu_anchor(base_url()."path/to/something", "menu_item")?>
- =menu_anchor(base_url()."path/to/something", "menu_item")?>
- =menu_anchor(base_url()."path/to/something", "menu_item")?>
- =menu_anchor(base_url()."path/to/something", "menu_item")?>
The css:
ul.topnav li.topnav-1 a.active { something clever }