Skip to content

Instantly share code, notes, and snippets.

@netdoctor
Last active December 12, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netdoctor/4699020 to your computer and use it in GitHub Desktop.
Save netdoctor/4699020 to your computer and use it in GitHub Desktop.
function to render a topbar menu in Processwire using the bootstrap framework. Assumes you have a text field called "navbar_separator" which can be used to create a bootstrap separator element above the $page and textfield called "menu_title" which we use to create an override of the $page->title to customize the menu.
<?php
/**
* renderResponsiveNavbar function for Processwire CMS.
*
* Author - Jeff Selser
* 01/20/2013
* @access public
* @param mixed $page
* @param string $brand (default: SProject Name')
* @param int $maxChilden (default: 15) sets limit on number of children in a dropdown
* @param int $levels (default: 2)
* @return void
* @TODO - Add support for three levels (2 is max now)
*/
function renderResponsiveNavbar($page, $brand='Project Name', $levels=2, $maxChildren=15){
$homepage = wire('pages')->get('/');
$cache = wire('modules')->get('MarkupCache');
// check the cache if not current (1day) build the menu, when testing set it to 0
if(!$navbar = $cache->get('navbar', 86400)){
// build the bootstrap html
$navbar = "<div class='navbar navbar-inverse'>\n";
$navbar .= "\t<div class='navbar-inner'>\n";
$navbar .= "\t\t<div class='container'>\n";
$navbar .= "\t\t\t<a class='btn btn-navbar' data-toggle='collapse' data-target='.nav-collapse'>\n";
$navbar .= "\t\t\t\t<span class='icon-bar'></span>\n";
$navbar .= "<span class='icon-bar'></span>\n";
$navbar .= "<span class='icon-bar'></span>\n";
$navbar .= "\t\t\t\t</a>";
// display the brand html if set
if ($brand) {
$navbar .= "<a class='brand visible-phone' href='/'>{$brand}</a>\n";
}
$navbar .= "<div class='nav-collapse collapse'>\n";
$ul = "<ul class='nav'>\n";
// loop through the children of the parent meny items.
foreach($homepage->children as $child) {
if($levels == 2 && $child->id > 1 && $child->numChildren && $child->numChildren < $maxChildren) {
// add data-target='{$child->url}' to anchor to click target
$li .= "\t<li class='dropdown'>" .
"<a href='{$child->url}' class='dropdown-toggle' data-toggle='dropdown' data-target='{$child->url}'>{$child->get("menu_title|title")}<b class='caret'></b></a>";
$li .= "\n\t\t<ul class='dropdown-menu'>\n";
foreach($child->children() as $c) {
if($c->navbar_separator){
$li .= "<li class='divider'></li>\n" .
"<li class='nav-header'>{$c->navbar_separator}</li>";
}
$li .= "\t\t\t<li><a href='{$c->url}'>{$c->get("menu_title|title")}</a></li>\n";
}
$li .= "</ul>";
} else {
$class = $child === $page->rootParent ? " class='active'" : '';
$li .= "\n\t<li$class>" .
"<a href='{$child->url}'>{$child->get("menu_title|title")}</a>";
}
}
$ul .= $li;
$ul .= "</ul>\n";
// pull a contact link to the right
$navbar .= $ul;
$navbar .= "<ul class='nav pull-right'>\n";
$navbar .= "\t\t<li><a href='/contact/'>Contact</a></li>\n";
$navbar .= "</ul>\n";
// Close out the divs
$navbar .= "</div>\n</div>\n</div>\n</div>";
}
// save the menu to the cache (if not the same)
$cache->save($navbar);
// return the HTML
return $navbar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment