• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Menu Generator

#1
[eluser]Unknown[/eluser]
Hi Guys!

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

Code:
<?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:

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

View

Code:
<nav>&lt;?php echo $menu; ?&gt;</nav>

or Template Parser
Code:
<nav>{menu}</nav>

#2
[eluser]Noobigniter[/eluser]
Thanks

#3
[eluser]Unknown[/eluser]
Looks very good. The problem is, how to get the menu items into an array from, let's say... a database (easily)?

#4
[eluser]Unknown[/eluser]

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

error
how to use ?

#5
[eluser]Noobigniter[/eluser]
Did you load string helper ?
Code:
$this->load->helper('string');

http://ellislab.com/codeigniter/user-gui...elper.html


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.