Skip to content

Instantly share code, notes, and snippets.

@kanonji
Created September 9, 2010 00:46
Show Gist options
  • Save kanonji/571144 to your computer and use it in GitHub Desktop.
Save kanonji/571144 to your computer and use it in GitHub Desktop.
/**
* Extended HtmlHelper
* @author kanonji
*/
class ExHtmlHelper extends HtmlHelper {
/**
* Extended HtmlHelper->link() to understand the url is current or not.
*
* $options['current'] is additional option key.
* If no value set, 'current' is set as default.
* example:
* $this->ExHtml->link('foo', 'bar', array('class'=>'baz', 'current'=>'qux'))
* result:
* <a href="/foo/" class="baz qux">bar</a>
*
* @param string $title The content to be wrapped by <a> tags.
* @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
* @param array $options Array of HTML attributes.
* @param string $confirmMessage JavaScript confirmation message.
* @return string An `<a />` element.
* @access public
* @link http://book.cakephp.org/view/1442/link
*/
public function link($title, $url = null, $options = array(), $confirmMessage = false) {
$current = 'current';
if(isset($options['current']))
$current = $options['current'];
unset($options['current']);
if ($url === null)
$str = Router::url($title);
else
$str = Router::url($url);
list($str) = explode('?', $str);
if(rtrim($str, '/') === rtrim(Router::url(""), '/'))
$options['class'] = isset($options['class']) ? "{$options['class']} {$current}" : $current;
return parent::link($title, $url, $options, $confirmMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment