UL导航构造(创建可重用的导航)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UL导航构造(创建可重用的导航)相关的知识,希望对你有一定的参考价值。
Aimed at designers who want to sprinkle in a little php to make development easier. If building/prototyping a site with html/PHP, this can save time by constructing the navigation list on the fly, based on re-usable navigation settings.Usage: Assuming you have multiple HTML/PHP pages, simply add $page definition to the top of each page, include the PHP files, then configure the settings to build your navigation. (See the detail of each of the three core pages below for more information.)
<?php // Create (at least) 2 pages: // 1) page.php : this is the rendered html page // 2) nav-settings.php : this holds the settings of number of nav items, urls, etc. ?> <!-- page.php ---------------------------------------- --> <html> <head> <!-- what page am i? --> <?php global $page; $page="PAGE_SHORTNAME or STRING FOR TITLE"; // see nav-settings.php ?> <?php include('nav-settings.php'); ?> <!-- will write title tag: see nav-settings.php --> </head> <body id="<?php echo $page; ?>"> <!-- optional: used for css styling purposes if necessary --> <div> <!-- insert navigation list here --> <?php echo $navbarList; ?> <!-- will produce something like this (assuming $page='ordering'): --> <!-- <ul> <li><a href="#">Home</a></li> <li><a href="#">Products</a></li> <li class="selected"><span>Ordering Info</span></li> <li><a href="#">Press Room</a></li> <li><a href="#">Events</a></li> <li><a href="#">Publications</a></li> <li><a href="#">Business Development</a></li> </ul> --> </div> </body> </html> <!-- nav-settings.php ---------------------------------------- --> <?php /* declare global variables ---------------------------------------- */ global $tab; // shortname of tab/page global $tabName; // long (display) name of tab/page global $page; // name of page for <title> global $navbarTabs; // array holding tab and tabNames global $navbarURLs; // array holding tab URLs global $navbarList; // string holding the navBar // make sure these arrays match! // (there are more efficient ways of building these arrays, but I am shooting for simplicity) "home" => "Home", "products" => "Products", "ordering" => "Ordering Info", "press" => "Press Room", "events" => "Events", "publications" => "Publications", "contract" => "Business Development" ); "home" => '#', // urls for the links "products" => '#', "ordering" => '#', "press" => '#', "events" => '#', "publications" => '#', "contract" => '#' ); // check current page, then determine name of current page // if given a non-shortname value, $page can be the custom name of the page if ( ($page == "home") || ($page == "products") || ($page == "events") || ($page == "ordering") || ($page == "press") || ($page == "publications") || ($page == "contract")) { $tabName = $navbarTabs[$page]; } else { $tabName = $page; } // create navigation list => $navbarList (echo it whenever you need the nav list) $navbarList = "<ul> "; // add id or class if needed for styling foreach ($navbarTabs as $k => $v) { switch ($k) { case $page: $navbarList = $navbarList."<li class="selected"><span>$v</span></li> "; break; default: $link = $navbarURLs[$k]; $navbarList = $navbarList."<li><a href="$link">$v</a></li> "; } } $navbarList = $navbarList."</ul>"; // print title tag on HTML page echo '<title>SITE NAME : '.$tabName.'</title>'; ?>
以上是关于UL导航构造(创建可重用的导航)的主要内容,如果未能解决你的问题,请参考以下文章