Skip to content

Instantly share code, notes, and snippets.

@druu
Created May 10, 2012 11:07
Show Gist options
  • Select an option

  • Save druu/2652454 to your computer and use it in GitHub Desktop.

Select an option

Save druu/2652454 to your computer and use it in GitHub Desktop.
Helper function for creating and highlighting Twitter Bootstrap Nav-Links
if (! function_exists('nav_link'))
{
/**
* Helper function for creating and highlighting Twitter Bootstrap Nav-Links
* david.wosnitza // 2012-05-10 13:05:34
*
* @param string $uri The URI you want to Navigate to
* @param string $title Your Link text
* @param boolean $home Activation override: Highlights on if URI is empty
* @param integer $level Choose the URI segment to comare to
* @return string The complet <LI>-Element
*/
function ch_navlink($uri, $title, $home = FALSE, $level = 1)
{
$CI = &get_instance();
$uri_orig = $uri;
$uri = $uri[0] === '/' ? substr($uri, 1) : $uri;
$uri_string = $CI->uri->uri_string();
$uri_array = explode('/', $uri);
$segment_count = $CI->uri->total_segments();
$uri_segment_count = count(explode('/', $uri));
$active = '';
if (
$segment_count === 1
OR (
$segment_count > 1
AND $segment_count === $uri_segment_count
)
)
{
$active = $uri_string === $uri ? 'class="active" ' : '';
}
elseif (
$segment_count > 1
AND $uri_string !== $uri)
{
$active = $CI->uri->segment($level) === $uri_array[$level-1] ? 'class="active" ' : '';
}
if ( $home && $segment_count == 0)
{
$active = ' class="active"';
}
return '<li '.$active.'><a href="'.$uri_orig.'">'.$title.'</a></li>';
}
}
/* End of file common_helper.php */
/* Location: ./application/helpers/common_helper.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment