Welcome Guest, Not a member yet? Register   Sign In
Insert actvie css-class in a menu
#1

[eluser]überfuzz[/eluser]
I have a menu where there are some css-classes that are showing active link. The owner of the site want to have a link marked as active as default. At the moment I muster this in the view-file like this:

Code:
<?php foreach ($menu AS $value) //the array menu is stated in config and holds; segment, name and atribute.
{
   $temp_seg = $this->uri->segment(1) ? $this->uri->segment(1) : 'home';
   $class = ($temp_seg == $value['segment']) ? 'active' : '';
}

?>

            <li class="&lt;?php echo $value['attribute']; echo $class; ?&gt;"><a href="&lt;?php echo base_url() . $value['segment']; ?&gt;" class="menu_font">&lt;?php echo $value['name']; ?&gt;</a></li>

I'm thinking about moving parts of this to a model called systemdata and the MY_Controller, but I'm not exactly 100% sure of how and where I should insert the active css-class. So I just have to echo-loop the menu in the view-file.

Some ideas I have:
1. Put the loop in the config-file. No, I don't like to mess about with loops in config.php
2. Render it in the system model, when it's fetched from config.
3. render it in the MY_Controller, when it's received from model(system.php).

2 and 3 would mean that I have to loop through the array twice along the way just to get the menu ouput in the view-file. I've got one argument for doing it in the model, it's how I usually do this.

The question would be something like, is there a nifty way of doing this..?
#2

[eluser]überfuzz[/eluser]
I could display the other way a was talking about, to explain my question better.
Code:
//view-file
        <ul>
&lt;?php foreach ($menu AS $value): ?&gt;
            <li class="&lt;?php echo $value['attribute']; echo $value['class']; ?&gt;"><a href="&lt;?php echo base_url() . $value['segment']; ?&gt;" class="menu_font">&lt;?php echo $value['name']; ?&gt;</a></li>
&lt;?php endforeach; ?&gt;
        </ul>
In the model I merge in some new keys.
Code:
//systemdata.php
        foreach($this->config->item('menu') AS $i => $value)
        {
            $temp_seg = $this->uri->segment(1) ? $this->uri->segment(1) : 'hem';
            $menu[$i]['class'] = ($temp_seg == $value['segment']) ? 'Selected' : '';
        }
        return $menu;




Theme © iAndrew 2016 - Forum software by © MyBB