EllisLab text mark
Advanced Search
     
Simple Menu Generator
Posted: 23 April 2011 06:04 PM   [ Ignore ]
Joined: 2011-04-23
1 posts

Hi Guys!

My first library for CI. Please, comments and suggestions.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Menu {

    
var $target         NULL;
    var 
$output         NULL;
    
    var 
$full_tag_open  '<ul>';
    var 
$full_tag_close '</ul>';
    var 
$item_tag_open  '<li>';
    var 
$item_tag_close '</li>';
    var 
$cur_page       '';
    var 
$cur_class      'selected';
    var 
$cur_display    TRUE;
    
    public function 
__construct()
    
{
        log_message
('debug'"Menu Library Initialized");       
    
}
    
    
private function _data()
    
{     
        $menu_data 
= array(
            
            
'header' => array(
                            array(
'name'  => 'Home'
                                  
'slug'  => '',
                                  
'class' => 'homepage'),
                
                            array(
'name'  => 'My Jobs'
                                  
'slug'  => 'work',
                                  
'class' => ''),
                
                            array(
'name'  => 'Talk to Me'
                                  
'slug'  => 'contact',
                                  
'class' => '')
            ),
            
            
'footer' => array(
                            array(
'name'  => 'About Us'
                                  
'slug'  => 'about',
                                  
'class' => '')
            )
            
        );
                
        if ( ! empty(
$menu_data))
        
{
            
if (array_key_exists($this->target$menu_data))
            
{
                
return $menu_data[$this->target];
            
}
        }
    }
    
    
private function _create_links()
    
{   
        $CI 
=& get_instance();
        
        
$this->cur_page trim_slashes($CI->uri->ruri_string());
        
$li NULL;
        
        foreach(
$this->_data() as $line)
        
{
            
if($this->cur_display == TRUE)
            
{
                
($this->cur_page == $line['slug']) ? $line['class'$line['class'].' '.$this->cur_class $line['class'];              
            
}
                        
            $li 
.= $this->item_tag_open.anchor($line['slug']$line['name'], array('class' => $line['class'])).$this->item_tag_close;
        
}
        
        $this
->output $this->full_tag_open.$li.$this->full_tag_close;
        
        return 
$this->output;     
    
}

    
    
public function render($input)
    
{
        $this
->target $input;
        
        if (
$this->target != NULL)
        
{
            
return $this->_create_links();
        
}
    }

}

/* End of file Menu.php */
/* Location: ./application/libraries/Menu.php */ 

How to use:

$this->load->library('menu');
        
$data = array(
    
'menu' =>  $this->menu->render('header')
); 

View

<nav><?php echo $menu?></nav

or Template Parser

<nav>{menu}</nav
 
Posted: 28 April 2011 02:07 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-13
77 posts

Thanks

 
Posted: 03 November 2012 06:58 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-03
1 posts

Looks very good. The problem is, how to get the menu items into an array from, let’s say… a database (easily)?

 
Posted: 03 November 2012 01:32 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-11-03
1 posts

Fatal error: Call to undefined function trim_slashes() in C:\xampp\htdocs\ci\application\libraries\menu.php on line 60

error
how to use ?

 
Posted: 03 November 2012 03:52 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-13
77 posts

Did you load string helper ?

$this->load->helper('string'); 

http://ellislab.com/codeigniter/user-guide/helpers/string_helper.html