Skip to content

Commit

Permalink
Add frameworks compatible Breadcrumbs.
Browse files Browse the repository at this point in the history
New options in HtmlHelper::getCrumbList() to make it compatible with Twitter Bootstrap, Zurb foundation or other CSS frameworks.
  • Loading branch information
planardothum committed Nov 14, 2012
1 parent bfbd055 commit a0f323e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
69 changes: 68 additions & 1 deletion lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,73 @@ public function testCrumbListFirstLink() {
);
}

/**
* test getCrumbList() in Twitter Bootstrap style.
*
*
* @return void
*/
public function testCrumbListBootstrapStyle() {

$this->Html->addCrumb('Home', '/', array('class'=>'home'));
$this->Html->addCrumb('Library', '/lib');
$this->Html->addCrumb('Data');
$result = $this->Html->getCrumbList(
array('class' => 'breadcrumb', 'separator'=>'<span class="divider">/</span>', 'firstClass'=>false, 'lastClass'=>'active')
);
$this->assertTags(
$result,
array(
array('ul' => array('class' => 'breadcrumb')),
'<li',
array('a' => array('href' => '/')), 'Home', '/a',
array('span'=>array('class'=>'divider')),'preg:/\//','/span',
'/li',
'<li',
array('a' => array('href' => '/lib')), 'Library', '/a',
array('span'=>array('class'=>'divider')),'preg:/\//','/span',
'/li',
array('li' => array('class' => 'active')),'Data','/li',
'/ul'
), true
);
}

/**
* Test GetCrumbList using style of Zurb Foundation.
*
* @return void
*/
public function testCrumbListZurbStyle() {

$this->Html->addCrumb('Home', '#');
$this->Html->addCrumb('Features', '#');
$this->Html->addCrumb('Gene Splicing', '#');
$this->Html->addCrumb('Home', '#');
$result = $this->Html->getCrumbList(
array( 'class'=>'breadcrumbs', 'firstClass'=>false, 'lastClass'=>'current')
);
$this->assertTags(
$result,
array(
array('ul' => array('class' => 'breadcrumbs')),
'<li',
array('a' => array('href' => '#')), 'Home', '/a',
'/li',
'<li',
array('a' => array('href' => '#')), 'Features', '/a',
'/li',
'<li',
array('a' => array('href' => '#')), 'Gene Splicing', '/a',
'/li',
array('li' => array('class' => 'current')),
array('a' => array('href' => '#')), 'Home', '/a',
'/li',
'/ul'
), true
);
}

/**
* testLoadConfig method
*
Expand Down Expand Up @@ -1932,4 +1999,4 @@ public function testParseAttributeCompact() {
$this->assertEquals('', $helper->parseAttributes(array('require' => false)));
}

}
}
24 changes: 21 additions & 3 deletions lib/Cake/View/Helper/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,13 +682,24 @@ public function getCrumbs($separator = '&raquo;', $startText = false) {
* similar to HtmlHelper::getCrumbs(), so it uses options which every
* crumb was added with.
*
* ### Options
* - `separator` Separator content to insert in between breadcrumbs, defaults to ''
* - `firstClass` Class for wrapper tag on the first breadcrumb, defaults to 'first'
* - `lastClass` Class for wrapper tag on current active page, defaults to 'last'
*
* @param array $options Array of html attributes to apply to the generated list elements.
* @param string|array|boolean $startText This will be the first crumb, if false it defaults to first crumb in array. Can
* also be an array, see `HtmlHelper::getCrumbs` for details.
* @return string breadcrumbs html list
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
*/
public function getCrumbList($options = array(), $startText = false) {
$defaults = array('firstClass'=>'first', 'lastClass'=>'last', 'separator' => '');
$options = array_merge($defaults, (array)$options);
$firstClass = $options['firstClass'];
$lastClass = $options['lastClass'];
$separator = $options['separator'];
unset($options['firstClass'], $options['lastClass'], $options['separator']);
$crumbs = $this->_prepareCrumbs($startText);
if (!empty($crumbs)) {
$result = '';
Expand All @@ -702,9 +713,16 @@ public function getCrumbList($options = array(), $startText = false) {
$elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
}
if (!$which) {
$options['class'] = 'first';
if ($firstClass !== false) {
$options['class'] = $firstClass;
}
} elseif ($which == $crumbCount - 1) {
$options['class'] = 'last';
if ($lastClass !== false) {
$options['class'] = $lastClass;
}
}
if (!empty($separator) && ($crumbCount - $which >= 2)) {
$elementContent .= $separator;
}
$result .= $this->tag('li', $elementContent, $options);
}
Expand Down Expand Up @@ -1211,4 +1229,4 @@ public function loadConfig($configFile, $path = null) {
return $configs;
}

}
}

0 comments on commit a0f323e

Please sign in to comment.