Welcome Guest, Not a member yet? Register   Sign In
Quick link building library
#1

[eluser]awpti[/eluser]
Code:
<?php
class Awpnav {
    
    var $CI;
    var $css_class = '';
    var $css_class_ns = '';
    var $input_menu = array();
    var $uri = '';
    
    function Awpnav()
    {
        $this->CI =& get_instance();
    }

    function build_links()
    {
        if(!is_array($this->input_menu))
        {
            return FALSE;
        }
        else
        {
            foreach($this->input_menu as $key => $value)
            {
                $class = '';
                if($this->uri == str_replace('/', '', $key))
                {
                    $class = 'class="'.$this->css_class.'" ';
                }
                elseif($this->css_class_ns !== '')
                {
                    $class = 'class="'.$this->css_class_ns.'" ';
                }
                $menu_final[] = '<a '.$class.'href="'.$key.'/">'.$value.'</a>';
            }
        }
        // Returns a simple array, nothing to see here.
        return $menu_final;
    }
}
?&gt;

Tabbing is a tad goofed, but it works quite well.

Very simple:

Code:
$this->awpnav->css_class = 'current';
$this->awpnav->css_class_ns = 'not_current'; //or leave it blank if none.
$this->awpnav->uri = $this->uri->segment(1);

$this->awpnav->input_menu = array(
        ''        => 'Home',
        '/blotter'    => 'TechBlotter',
        '/devblog'    => 'DevBlog',
        '/about'    => 'About',
        '/contact'    => 'Contact'
        );

$data['my_links'] = $this->awpnav->build_links(); //Returns an array

This added about 0.003 seconds to the page render time - don't know if that's horrible, but it seems to run well under a load.

Hope someone finds this useful in some way! This is my first-ever foray into developing my own library. Comments are appreciated even if you don't have a use for it. I'd love to know what I could do better and what, if any, bad practices I may be approaching.

You can see it in-action on onlytechnews.com and the source here:

http://onlytechnews.com/source/libraries/Otnnav.phps
http://onlytechnews.com/source/controlle...front.phps

These are symlinks, so any changes I make are reflected instantly.

Cheers.
#2

[eluser]xwero[/eluser]
From my point of view i wouldn't do this in a class but as a helper. There are a few reasons for this

- i think the menu will not be build in the controller
- if you put awpmenu() in the view it can take advantage of the the current uri_string and compare it with the links
- i think creating html should be done using a view file for more configuration

But i say it again my view on things your method works already.
#3

[eluser]dawnerd[/eluser]
Yea, I agree with xwero. Html should be produced in the view file. It;s really bad habit to put html in a function.




Theme © iAndrew 2016 - Forum software by © MyBB