From a0f323eb4cfe3238f79e59119360f99f697e11bb Mon Sep 17 00:00:00 2001 From: planardothum Date: Tue, 13 Nov 2012 20:19:29 -0500 Subject: [PATCH] Add frameworks compatible Breadcrumbs. New options in HtmlHelper::getCrumbList() to make it compatible with Twitter Bootstrap, Zurb foundation or other CSS frameworks. --- .../Test/Case/View/Helper/HtmlHelperTest.php | 69 ++++++++++++++++++- lib/Cake/View/Helper/HtmlHelper.php | 24 ++++++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php index fcb299071..7170d01ae 100644 --- a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php @@ -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'=>'/', 'firstClass'=>false, 'lastClass'=>'active') + ); + $this->assertTags( + $result, + array( + array('ul' => array('class' => 'breadcrumb')), + ' array('href' => '/')), 'Home', '/a', + array('span'=>array('class'=>'divider')),'preg:/\//','/span', + '/li', + ' 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')), + ' array('href' => '#')), 'Home', '/a', + '/li', + ' array('href' => '#')), 'Features', '/a', + '/li', + ' 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))); } -} +} \ No newline at end of file diff --git a/lib/Cake/View/Helper/HtmlHelper.php b/lib/Cake/View/Helper/HtmlHelper.php index c4f6c76ff..583368fd8 100644 --- a/lib/Cake/View/Helper/HtmlHelper.php +++ b/lib/Cake/View/Helper/HtmlHelper.php @@ -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; } -} +} \ No newline at end of file