Уровень 1
function activeClass( $page, $current = 'index') {
if( $page == $current){
return ' active';
}
return ' inactive';
}
Использование:
<ul class="nav">
<li class="nav-active<?php echo activeNav( 'home', $page); ?>"><a href="/" title="Home Page">Home</a></li>
<li class="nav-active<?php echo activeNav( 'another', $page); ?>"><a href="/" title="Another Page Page">Another Page</a></li>
</ul>
Уровень 2
function printNavigationElement( $page, $title, $current = 'index'){
$title = htmlspecialchars( $title);
return '<li class="nav-active' . activePage( $page, $current) . '"><a href="/' . htmlspecialchars($page) .
'" title="' . $title . '">' . $title . '</a></li>';
}
Использование:
<ul class="nav">
<?php
echo printNavigationElement( 'home', 'Home page', $page);
echo printNavigationElement( 'another', 'Another page', $page);
?>
</ul>
Уровень 3 - Упс
class NavigationItem {
public $url;
public $title;
public function __construct( $url, $title){
$this->url = $url;
$this->title = $title;
}
}
class Navigation {
// You should use setters and getters with protected variables
public $current = '';
public $items = array()
public function display() {
echo '<ul class="nav">';
foreach( $this->items as $item){
printNavigationElement( $item->url, $item->title, $this->current);
}
echo '</ul>'
}
}
Использование:
<?php
$navigation = new Navigation();
$navigation->current = $page;
$navigation->items[] = new NavigationItem( 'home', 'Home page');
$navigation->items[] = new NavigationItem( 'another', 'Another page');
$navigation->display();