Skip to content

Instantly share code, notes, and snippets.

@jpdelatorre
Created December 11, 2009 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpdelatorre/253926 to your computer and use it in GitHub Desktop.
Save jpdelatorre/253926 to your computer and use it in GitHub Desktop.
A CakePHP helper that displays navigation dynamically. It detects what page you're in and adds a class "active" to the list element.
<?php
class DisplayHelper extends AppHelper {
var $helpers = array('Html');
/**
*
* Display navigation and detects currect page and adds 'active' class
* to the <li> tag
*
*/
function navigation() {
$nav = array(
'Home' => '/',
'About' => 'about',
'Login' => 'users/login',
'Sign up' => 'users/signup'
);
$url_string = $this->params['url']['url'];
$output = '<ul>';
foreach($nav as $label => $url) {
$li_class = '';
if(($url == $url_string) || ((false !== strpos($url_string, $url) && $url != '/'))) {
$li_class = ' class="active"';
}
$output .= '<li' . $li_class . '>' . $this->Html->link($label, '/'.$url) . '</li>';
}
$output .= '</ul>';
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment