Created
July 8, 2013 11:55
-
-
Save bmcminn/5948132 to your computer and use it in GitHub Desktop.
Simple PHP functions to generate Twitter bootstrap components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generates a Twitter Bootstrap menu markup, complete with Dropdown menus as defined in sitedata.json | |
* @param array $data JSON data converted to PHP array in siteconfig.php | |
* @param boolean $settings Overrideable settings array to provide some options | |
*/ | |
function add_menu($data, $settings = false) { | |
$menu = $menu_content = $active = $inverse = $pull = $color = ''; | |
// Set active link test string | |
$currentURL = preg_replace('/[\w\d-_:\.\#\%\?\/]*?(\/[\w\d-_\.+=\#\%\?]*\.(php|html|htm))\S*/', '$1', $_SERVER['SCRIPT_FILENAME']); | |
// These settings can be overridden provided the $settings var is passed | |
$master_settings = [ | |
"inverse" => false | |
, "pull" => false | |
, "color" => null | |
]; | |
// Merge our settings array | |
if ($settings) { | |
$settings = array_unique(array_merge($master_settings, $settings)); | |
// Set color profile of navmenu | |
if ($settings['color']) { $color = $settings['color']; } | |
// Render inverse | |
if ($settings['inverse']) { $inverse = 'navbar-inverse'; } | |
// Determine what direction to pull the menu_content | |
if ($settings['pull']) { | |
switch ($settings->pull) { | |
case 'right': | |
$pull = 'pull-right'; | |
break; | |
case 'left': | |
$pull = 'pull-left'; | |
break; | |
} | |
} | |
} // if ($settings) { ... } | |
// Output the .ui-group styles | |
foreach ($data as $link) { | |
$active = ''; | |
// Set active link | |
if ($link->url === $currentURL) { $active = 'active'; } | |
// Generate submenus | |
if (isset($link->sublinks)) { | |
// Build sublinks container | |
$menu_content .= "</ul>" | |
. "<ul class=\"nav\">" | |
. "<li class=\"dropdown $active\">" | |
. "<a href=\"$link->url\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">" | |
. "$link->name <b class=\"caret\"></b>" | |
. "</a>" | |
. "<ul class=\"dropdown-menu\">" | |
; | |
// Render sublinks | |
foreach ($link->sublinks as $sublink) { | |
$menu_content .= "<li><a href=\"" . SITE_LOCATION . "$sublink->url\" title=\"$sublink->link_title\">$sublink->name</a>"; | |
} | |
// Close sublinks container | |
$menu_content .= "</ul>" | |
. "</li>" | |
. "</ul>" | |
. "<ul class=\"nav\">" | |
; | |
} else { | |
$menu_content .= "<li class=\"$active\"><a href=\"" . SITE_LOCATION . "$link->url\" title=\"$link->link_title\">$link->name</a></li>"; | |
} | |
} // foreach ($data as $link) | |
$menu = "<nav class=\"navbar $inverse $color top-navbar\" role=\"navigation\">" | |
. "<div class=\"navbar-inner pull-right\">" | |
. "<ul class=\"nav\">" | |
. $menu_content | |
. "</ul>" | |
. "</div>" | |
. "</nav><!-- .top-navbar -->" | |
; | |
echo $menu; | |
} // add_menu() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment