Add frameworks compatible Breadcrumbs.

New options in HtmlHelper::getCrumbList() to make it compatible with Twitter Bootstrap, Zurb foundation or other CSS frameworks.
This commit is contained in:
planardothum 2012-11-13 20:19:29 -05:00
parent bfbd05576b
commit a0f323eb4c
2 changed files with 89 additions and 4 deletions

View file

@ -1850,6 +1850,73 @@ class HtmlHelperTest extends CakeTestCase {
);
}
/**
* 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
*
@ -1932,4 +1999,4 @@ class HtmlHelperTest extends CakeTestCase {
$this->assertEquals('', $helper->parseAttributes(array('require' => false)));
}
}
}

View file

@ -682,6 +682,11 @@ class HtmlHelper extends AppHelper {
* 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.
@ -689,6 +694,12 @@ class HtmlHelper extends AppHelper {
* @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 = '';
@ -702,9 +713,16 @@ class HtmlHelper extends AppHelper {
$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);
}
@ -1211,4 +1229,4 @@ class HtmlHelper extends AppHelper {
return $configs;
}
}
}