Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by CI TaMeR )

DynamicMenu

Category:Help -> TipsAndTricks


Updating my contributions on the wiki has become a nightmare.
Please check my svn at google for the latest revisions:
http://code.google.com/p/tamer/
CI_TaMeR SVN

Menu library.
Will show/hide sub-menus depending on in which, uri->segment the user is browsing.
Just add it in to autoload.

I call it from the view file like this:

$menu $this->menu->mkmenu(); 

I use a header.php and footer.php so I have to do this only once in my header.php. In any case where ever in the view file you like to have your menu you would enter this:
Make sure you also load the html helper for ul

<?=ul($menu,array('class' => 'Navbar','id' => 'Navbar'));?> 

And here you create your menu

class Menu
{
    
function setVars()
    
{
        $uid 
$this->db_session->userdata('uid');

    
$this->menu = array(
           
'1' => anchor('news''News'),
           
'4' => anchor('ticket''Support'),
           
'5' => anchor('test''Test'),
    );

        if(
$this->uid 0){
            $this
->['3'anchor('contact/person''Profile');
            
$this->sub['contact'= array(
                
anchor('contact/person','User'),
                
anchor('contact/company','Comapny'),
                
anchor('contact/secquestion','Security'),
                
anchor('contact/password','Password')
                );
    
}       
        
        $this
->sub['news'= array(anchor('news/post','Post'));
        
    
}
    
    
function Menu()
    
{
        log_message
('debug''Menu: initialized');
    
}
    
    
function mkMenu()
    
{
        $CI 
=& get_instance();
        
$this->uid $CI->db_session->userdata('uid');
        
$this->setVars();
        
ksort($this->menu);
        
$segment $CI->uri->segment(1);
        foreach(
$this->menu as $k => $v){
            $menutmp[$k] 
$v;
            if (
strripos($v$segment) !== FALSE && isset($this->sub[$segment])){
                $this
->menu[$this->menu[$k]] $this->sub[$segment];
                unset(
$menutmp[$k]);
                
$menutmp[$this->menu[$k]] $this->sub[$segment];
            
}
        }
        
return $menutmp;
    
}

Here is the style code:

#HeaderNav {
border1px solid #000000;
background#333333;
color#ffffff;
}

#HeaderNav a {
border1px solid #000000;
background#333333;
color#ffffff;
}

#HeaderNav a:hover {
border1px solid #000000;
background#333333;
color#ffffff;
}

#Navbar {

}

#Navbar ul.menu {
/* font-family: Verdana, Arial, Helvetica, sans-serif; */
font-size.8em;
font-stylenormal;
line-height1.3em;
font-weightnormal;
font-variantnormal;
text-transformnone;
color#00CC33;
text-decorationnone;
background-color#CCCCCC;
text-indent0em;
/* list-style-position: inside; */
list-style-positionoutside;
list-
style-typenone;
padding0 0 0 0;
margin0 0 0 0;
}

#Navbar li {
margin0 0 0 0;
list-
style-typenone;
}
#Navbar a {
displayblock;
padding2px 2px 2px 1em;
border1px solid #000000;
background#dcdcdc;
text-decorationnone/*remove the link underlines*/
}

#Navbar a:link, 
#list-menu a:active, 
#list-menu a:visited {
color#000000;
}

#Navbar a:hover {
border1px solid #000000;
background#333333;
color#ffffff;

If you make an improvement or have questions post them here:

Dynamic Menu Forum

Categories: