Skip to content

Instantly share code, notes, and snippets.

@Gumster
Last active June 21, 2016 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gumster/1464de34eaa739e3c6eb to your computer and use it in GitHub Desktop.
Save Gumster/1464de34eaa739e3c6eb to your computer and use it in GitHub Desktop.
/*
* Bootstrap menu helper
* renders menu and optionally 1 sub menu deep
* Config array is same as for fuel_nav()
*/
function bootstrap_menu($config = NULL, $float = NULL, $toggle = TRUE) {
$menu = (is_array($config)) ? fuel_nav(array_merge($config, array('render_type' => 'array'))) : fuel_nav(array('render_type' => 'array'));
if (is_array($menu)) {
$container_class = isset($config['container_tag_class']) ? 'nav navbar-nav ' . $config['container_tag_class'] : 'nav navbar-nav';
$container_id = isset($config['container_tag_id']) ? ' id="' . $config['container_tag_id'] . '"' : '';
if (isset($float))
$container_class .= ($float == 'left') ? ' navbar-left' : ' navbar-right';
$container_start = '<ul class="' . $container_class . '"' . $container_id . '>';
$container_end = '</ul>';
$list = '';
$counter = 0;
$last = 0;
$data_toggle = $toggle ? ' data-toggle="dropdown" ' : NULL;
// find num of displayable links
foreach ($menu as $show) {
if ($show['hidden'] == 'no')
$last++;
}
foreach ($menu as $item) {
if ($item['hidden'] == 'no') {
$classes = ($counter == 0) ? 'first ' : '';
$classes .= ($counter == ($last - 1)) ? 'last ' : '';
$classes .= (strpos('/'.uri_string().'/', $item['location'].'/') === 1 ) ? ' active' : ''; // good enough for navbar purposes where only top level visible
if (isset($item['children'])) {
//NB removing data-toggle="dropdown" from the <a> element allows link to be followed
$attributes = empty($item['attributes']) ? ' class="dropdown-toggle"'.$data_toggle : ' class="dropdown-toggle"'.$data_toggle.$item['attributes'];
$link = '<li class="dropdown' . $classes . '"><a href="'.base_url($item['location']).'"'.$attributes.'>'. $item['label'].' <b class="caret"></b></a>';
if (is_array($item['children'])) {
$sublist = '';
$sub_container_start = '<ul class="dropdown-menu">';
$sub_container_end = $container_end;
foreach ($item['children'] as $sub_item) {
$sublist .= '<li>' . anchor($sub_item['location'], $sub_item['label'], $sub_item['attributes']) . '</li>';
}
$sublist = $sub_container_start . $sublist . $sub_container_end;
$link .= $sublist . '</li>';
}
} else {
$link = '<li class="' . trim($classes) . '">' . anchor($item['location'], $item['label'], $item['attributes']) . '</li>';
}
$list .= $link;
$counter++;
}
}
return $container_start . $list . $container_end;
} else {
return '<!-- no menu data -->';
}
}
/**
* Usage
*/
<?php echo bootstrap_menu(array('container_tag_id' => 'main-nav'), 'right', FALSE); // last arg switches toggle behaviour for drop-downs ?>
/**
* CSS
* Hover on navbar - over-rides stock navbar behaviour
*/
ul.nav li.dropdown:hover > ul.dropdown-menu { display: block; }
/* & when menu collapsed */
.in ul.nav li.dropdown:hover > ul.dropdown-menu {
position:relative;
width:100%;
clear:both;
height:auto;
float:none;
margin-bottom:0;
}
.in ul.nav li.dropdown:hover > ul.dropdown-menu a {font-size:0.9em; padding-left:2em}
.navbar li:hover,
.dropdown-menu > li > a:hover,
.dropdown-menu > li > a:focus {background: #ECECEC; color:#fff;}
.navbar li:hover > .dropdown-menu {background: #ECECEC;}
.dropdown-menu, .navbar li.active:hover .dropdown-menu {border-top:0; box-shadow: none; background: #EEEEEE; border-color:#EEEEEE;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment