Welcome Guest, Not a member yet? Register   Sign In
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>


Messages In This Thread
Simple Menu Generator - by El Forum - 04-23-2011, 03:04 PM
Simple Menu Generator - by El Forum - 04-27-2011, 11:07 PM
Simple Menu Generator - by El Forum - 11-03-2012, 03:58 AM
Simple Menu Generator - by El Forum - 11-03-2012, 10:32 AM
Simple Menu Generator - by El Forum - 11-03-2012, 12:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB