Skip to content

Instantly share code, notes, and snippets.

@kaiohken1982
Created October 3, 2013 04:39
Show Gist options
  • Select an option

  • Save kaiohken1982/6805083 to your computer and use it in GitHub Desktop.

Select an option

Save kaiohken1982/6805083 to your computer and use it in GitHub Desktop.
ZF2 navigation using custom rendering function example
if(!function_exists('renderMenu')) {
function renderMenu($container, $obj, $step = 0) {
$html = '';
if(null !== $container && $container->count()) {
if(!$step) {
$html.= '<ul class="nav ">' . PHP_EOL;
$step++;
} elseif($step == 1) {
$html.= '<ul class="dropdown-menu">' . PHP_EOL;
$step++;
} else {
$html.= '<ul class="dropdown-menu sub-menu">' . PHP_EOL;
}
foreach($container AS $page) {
if(!$page->isVisible()) continue;
// MISSING check if the page must be rendered using ACL rules
$class = array();
if(null !== $page->class) {
$class[] = $page->class;
}
if($page->isActive(true)) {
$class[] = "active";
}
if($page->count() > 0) {
$class[] = "dropdown";
}
$class = implode(" ", $class);
if($page->count() > 0) {
$html.= '<li class="' . $class .'"><a class="dropdown-toggle" data-toggle="dropdown">' . $obj->translate($page->label);
if($step == 1) {
$html.= '<b class="caret"></b></a>';
} else {
$html.= '<i >&nbsp;</i><i class="icon-arrow-right"></i></a>';
}
} else {
$html.= '<li class="' . $class .'"><a href="' . $page->getHref() . '" >' . $obj->translate($page->getLabel()) . '</a>';
//$html.= $obj->navigation()->menu()->htmlify($page, false, false, false);
}
// Subpages, if any
if($page->count() > 0) {
$html.= renderMenu($page, $obj, $step);
}
$html.= '</li>' . PHP_EOL;
if(!$step) {
$html.= '<li class="divider-vertical"></li>' . PHP_EOL;
}
}
$html.= '</ul>' . PHP_EOL;
}
return $html;
}
}
echo renderMenu($this->container->findByLabel('Dashboard'), $this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment