Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add frameworks compatible Breadcrumbs.

New options in HtmlHelper::getCrumbList() to make it compatible with Twitter Bootstrap, Zurb foundation or other CSS frameworks.
  • Loading branch information...
commit a0f323eb4cfe3238f79e59119360f99f697e11bb 1 parent bfbd055
@planardothum planardothum authored
View
69 lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
@@ -1851,6 +1851,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
*
* @return void
@@ -1932,4 +1999,4 @@ public function testParseAttributeCompact() {
$this->assertEquals('', $helper->parseAttributes(array('require' => false)));
}
-}
+}
View
24 lib/Cake/View/Helper/HtmlHelper.php
@@ -682,6 +682,11 @@ 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.
@@ -689,6 +694,12 @@ public function getCrumbs($separator = '&raquo;', $startText = false) {
* @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 @@ 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);
}
@@ -1211,4 +1229,4 @@ public function loadConfig($configFile, $path = null) {
return $configs;
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.