Skip to content

Instantly share code, notes, and snippets.

@voku
Created June 4, 2014 08:41
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 voku/641b7478a9f49b76d783 to your computer and use it in GitHub Desktop.
Save voku/641b7478a9f49b76d783 to your computer and use it in GitHub Desktop.
top-navi for the bootstrap-framework: from array to ul/li - DEMO: http://ideone.com/9udES5
/**
* get the top-navi for the bootstrap-framework (HTML)
*
* @param $naviArray
* @param $tmpArrayChildOf
*
* @return string
*/
function getHTMLNaviBootstrap($naviArray, $tmpArrayChildOf)
{
$naviString = '';
$depth = 0;
foreach ($naviArray as $index => $item) {
$hasDropDown = false;
$newDepth = (int)$item['level'];
// is the navi-element "active"?
if (in_array($item['id'], $tmpArrayChildOf, true)) {
$tmpClass = ' active ';
} else {
$tmpClass = '';
}
// add template-class
$tmpClass .= ' menu_' . $item['template'] . ' ' . ' menu_pageid_' . $item['id'] . ' ';
// check for sub-"ul"
foreach ($naviArray as $item_tmp) {
if ($item['id'] == $item_tmp['root_id']) {
$tmpClass .= ' dropdown ';
$hasDropDown = true;
break;
}
}
if ($newDepth > $depth) {
while ($newDepth > $depth) {
if ($depth > 0) {
$ul_extra = 'mainnavSub dropdown-menu';
$li_extra = '';
} else {
$ul_extra = 'nav navbar-nav';
$li_extra = 'mainnav';
}
$naviString .= '
<ul class="' . $ul_extra . '">
<li class="' . $li_extra . ' ' . $tmpClass . '">';
$depth++;
}
} else if ($newDepth < $depth) {
while ($newDepth < $depth) {
$naviString .= '
</li>
</ul>';
$depth--;
}
$naviString .= '
</li>
<li class="mainnav ' . $tmpClass . '">';
} else if ($newDepth === $depth) {
if ($index === 0) {
$naviString .= '
<ul>
<li class="mainnav ' . $tmpClass . '">';
} else if ($newDepth == 1) {
$naviString .= '
</li>
<li class="mainnav ' . $tmpClass . '">';
} else {
$naviString .= '
</li>
<li class="' . $tmpClass . '">';
}
}
if ($hasDropDown === true) {
$extrasForLink = 'class="dropdown-toggle" data-toggle="dropdown"';
$extraIconForLink = '<b class="caret"></b>';
} else {
$extrasForLink = '';
$extraIconForLink = '';
}
$naviString .= '<a ' . $extrasForLink . ' href="' . $item['url'] . '">' . $item['name'] . ' ' . $extraIconForLink . '</a>';
$depth = $newDepth;
}
if (count($naviArray) > 0) {
do {
$naviString .= '
</li>
</ul>';
$depth--;
}
while ($depth > 0);
}
return $naviString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment