Where to store a common variable - El Forum - 02-11-2011
[eluser]ramm[/eluser]
Hi
I'm working on a simple navigation helper to place a navigation menu anywhere i need it and having the current section selected, and i need a way to define the array with the menu structure to pass it to the helper.
Where should i save that array to be available for the helper?
It will look something like this:
Code: $items = array(
'home' => array(
'id' => 'home',
'title' => 'Home',
'link' => ''
),
'about' => array(
'id' => 'about',
'title' => 'About Us',
'link' => 'aboutus'
),
'services' => array(
'id' => 'service',
'title' => 'Services',
'link' => 'services'
),
'contact' => array(
'id' => 'contact',
'title' => 'Contact Us',
'link' => 'contact'
)
);
Then i can call the menu:
Code: <?php echo menu('about'); ?>
Or in the controller
Code: $data['menu'] = menu('about');
And it will show the complete menu with the 'about' li set with the 'current' class.
Any ideas?
Where to store a common variable - El Forum - 02-12-2011
[eluser]Jaketoolson[/eluser]
I'm working on the same... I have sub menus and therefore my arrays wouldn't contain as much information as yours.
Code: $nav = array(
'home' => array(//sub-menu
'about', 'contact'
),
'stuff' => array(//submenu
'more', 'pages'
),
);
And then the value would also serve as the id and link... i created a nav cfg file and autoload it.
Where to store a common variable - El Forum - 02-12-2011
[eluser]ramm[/eluser]
Nice!
I did what you said and it worked fine.
This is my config file: (The ids are optional)
Code: $config['navigation'] = array(
'home' => array(
'id' => 'home',
'title' => 'Home',
'link' => ''
),
'about' => array(
'id' => 'about',
'title' => 'About Us',
'link' => 'about'
),
'contact' => array(
'title' => 'Contact Us',
'link' => 'contact'
)
);
And here is the helper code
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* menu_ul
* Returns a unordered list with the selected item as current
*/
if ( ! function_exists('menu_ul'))
{
function menu_ul($sel = 'home')
{
$CI =& get_instance();
$items = $CI->config->item('navigation');
$menu = '<ul class="main_nav">'."\n";
foreach($items as $item)
{
$current = (in_array($sel, $item)) ? ' class="current"' : '';
$id = (!empty($item['id'])) ? ' id="'.$item['id'].'"' : '';
$menu .= '<li'.$current.'><a href="'.$item['link'].'">'.$item['title'].'</a></li>'."\n";
}
$menu .= '</ul>';
return $menu;
}
}
// ------------------------------------------------------------------------
/**
* menu_p
* Returns a paragraph with the selected item as current
*/
if ( ! function_exists('menu_p'))
{
function menu_p($sel = 'home', $separator = '')
{
$CI =& get_instance();
$items = $CI->config->item('navigation');
$count = count($items);
$i = 0;
$menu = "\n".'<p class="bottom_nav">';
foreach($items as $item)
{
$current = (in_array($sel, $item)) ? ' class="current"' : '';
$id = (!empty($item['id'])) ? ' id="'.$item['id'].'"' : '';
$menu .= '<a'.$current.' href="'.$item['link'].'"'.$id.'>'.$item['title'].'</a>';
$i++;
if($count != $i)
{
$menu .= ' '.$separator.' ';
}
}
$menu .= '</p>'."\n";
return $menu;
}
}
/* End of file navigation_helper.php */
/* Location: ./system/helpers/navigation_helper.php */
Use
Code: <?php echo menu_ul('about'); ?>
//Outputs an unordered list with the <li class="current"> for the about link
Code: <?php echo menu_p('about', '|'); ?>
//Second parameter is separator, optional
Still not sure if i'm loading the config file correctly, but it works fine.
Thank you.
|