Skip to content
This repository

Frameworks compatible Breadcrumbs. #958

Merged
merged 3 commits into from over 1 year ago

4 participants

Harold Putman Mark Story Ber Clausen ADmad
Harold Putman

Add new options to HtmlHelper::getCrumbList() to allow it to work with Twitter Bootstrap, Zurb Foundation, and other CSS Frameworks.

  • 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'

Existing unit tests pass with no changes. New tests added to check the new functionality.

This is a reworking of pull request 952. The Pagination portion of that request has already implemented by someone else.

Harold Putman Add frameworks compatible Breadcrumbs.
New options in HtmlHelper::getCrumbList() to make it compatible with Twitter Bootstrap, Zurb foundation or other CSS frameworks.
a0f323e
lib/Cake/View/Helper/HtmlHelper.php
@@ -689,6 +694,12 @@ public function getCrumbs($separator = '»', $startText = false) {
689 694
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
690 695
  */
691 696
 	public function getCrumbList($options = array(), $startText = false) {
  697
+		$defaults = array('firstClass'=>'first', 'lastClass'=>'last', 'separator' => '');
1
Mark Story Owner

You should have spaces around =>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
@@ -1851,6 +1851,73 @@ public function testCrumbListFirstLink() {
1851 1851
 	}
1852 1852
 
1853 1853
 /**
  1854
+ * test getCrumbList() in Twitter Bootstrap style.
  1855
+ *
  1856
+ *
  1857
+ * @return void
  1858
+ */
  1859
+	public function testCrumbListBootstrapStyle() {
  1860
+
  1861
+		$this->Html->addCrumb('Home', '/', array('class'=>'home'));
1
Mark Story Owner

No blank new line needed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
((8 lines not shown))
  1858
+ */
  1859
+	public function testCrumbListBootstrapStyle() {
  1860
+
  1861
+		$this->Html->addCrumb('Home', '/', array('class'=>'home'));
  1862
+		$this->Html->addCrumb('Library', '/lib');
  1863
+		$this->Html->addCrumb('Data');
  1864
+		$result = $this->Html->getCrumbList(
  1865
+			array('class' => 'breadcrumb', 'separator'=>'<span class="divider">/</span>', 'firstClass'=>false, 'lastClass'=>'active')
  1866
+		);
  1867
+		$this->assertTags(
  1868
+			$result,
  1869
+			array(
  1870
+				array('ul' => array('class' => 'breadcrumb')),
  1871
+				'<li',
  1872
+				array('a' => array('href' => '/')), 'Home', '/a',
  1873
+				array('span'=>array('class'=>'divider')),'preg:/\//','/span',
1
Mark Story Owner

More spaces need around the operators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mark Story
Owner

I think this is pretty good, anyone see any reasons not to merge it into 2.3?

ADmad ADmad commented on the diff
lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
@@ -1851,6 +1851,71 @@ public function testCrumbListFirstLink() {
1851 1851
 	}
1852 1852
 
1853 1853
 /**
  1854
+ * test getCrumbList() in Twitter Bootstrap style.
  1855
+ *
  1856
+ *
1
ADmad Collaborator
ADmad added a note

Nitpick, extra blank line :smile:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
((30 lines not shown))
  1880
+			), true
  1881
+		);
  1882
+	}
  1883
+
  1884
+/**
  1885
+ * Test GetCrumbList using style of Zurb Foundation.
  1886
+ * 
  1887
+ * @return void
  1888
+ */
  1889
+	public function testCrumbListZurbStyle() {
  1890
+		$this->Html->addCrumb('Home', '#');
  1891
+		$this->Html->addCrumb('Features', '#');
  1892
+		$this->Html->addCrumb('Gene Splicing', '#'); 
  1893
+		$this->Html->addCrumb('Home', '#'); 
  1894
+		$result = $this->Html->getCrumbList(
  1895
+			array( 'class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current')
1
ADmad Collaborator
ADmad added a note

Extra space after array(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mark Story markstory merged commit c752b75 into from
Mark Story markstory closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Nov 13, 2012
Harold Putman Add frameworks compatible Breadcrumbs.
New options in HtmlHelper::getCrumbList() to make it compatible with Twitter Bootstrap, Zurb foundation or other CSS frameworks.
a0f323e
Nov 15, 2012
Harold Putman Fix spaces around operators. bed9ba2
Nov 16, 2012
Harold Putman Change more spacing issues. 4351b78
This page is out of date. Refresh to see the latest.
66  lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
@@ -1851,6 +1851,70 @@ public function testCrumbListFirstLink() {
1851 1851
 	}
1852 1852
 
1853 1853
 /**
  1854
+ * test getCrumbList() in Twitter Bootstrap style.
  1855
+ *
  1856
+ * @return void
  1857
+ */
  1858
+	public function testCrumbListBootstrapStyle() {
  1859
+		$this->Html->addCrumb('Home', '/', array('class'=>'home'));
  1860
+		$this->Html->addCrumb('Library', '/lib');
  1861
+		$this->Html->addCrumb('Data');
  1862
+		$result = $this->Html->getCrumbList(
  1863
+			array('class' => 'breadcrumb', 'separator' => '<span class="divider">/</span>', 'firstClass' => false, 'lastClass' => 'active')
  1864
+		);
  1865
+		$this->assertTags(
  1866
+			$result,
  1867
+			array(
  1868
+				array('ul' => array('class' => 'breadcrumb')),
  1869
+				'<li',
  1870
+				array('a' => array('href' => '/')), 'Home', '/a',
  1871
+				array('span' =>array('class' => 'divider')), 'preg:/\//', '/span',
  1872
+				'/li',
  1873
+				'<li',
  1874
+				array('a' => array('href' => '/lib')), 'Library', '/a',
  1875
+				array('span' => array('class' => 'divider')), 'preg:/\//', '/span',
  1876
+				'/li',
  1877
+				array('li' => array('class' => 'active')), 'Data', '/li',
  1878
+				'/ul'
  1879
+			), true
  1880
+		);
  1881
+	}
  1882
+
  1883
+/**
  1884
+ * Test GetCrumbList using style of Zurb Foundation.
  1885
+ * 
  1886
+ * @return void
  1887
+ */
  1888
+	public function testCrumbListZurbStyle() {
  1889
+		$this->Html->addCrumb('Home', '#');
  1890
+		$this->Html->addCrumb('Features', '#');
  1891
+		$this->Html->addCrumb('Gene Splicing', '#'); 
  1892
+		$this->Html->addCrumb('Home', '#'); 
  1893
+		$result = $this->Html->getCrumbList(
  1894
+			array('class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current')
  1895
+		);
  1896
+		$this->assertTags(
  1897
+			$result,
  1898
+			array(
  1899
+				array('ul' => array('class' => 'breadcrumbs')),
  1900
+				'<li',
  1901
+				array('a' => array('href' => '#')), 'Home', '/a',
  1902
+				'/li',
  1903
+				'<li',
  1904
+				array('a' => array('href' => '#')), 'Features', '/a',
  1905
+				'/li',
  1906
+				'<li',
  1907
+				array('a' => array('href' => '#')), 'Gene Splicing', '/a',
  1908
+				'/li',
  1909
+				array('li' => array('class' => 'current')),
  1910
+				array('a' => array('href' => '#')), 'Home', '/a',
  1911
+				'/li',
  1912
+				'/ul'
  1913
+			), true
  1914
+		);
  1915
+	}
  1916
+
  1917
+/**
1854 1918
  * testLoadConfig method
1855 1919
  *
1856 1920
  * @return void
@@ -1932,4 +1996,4 @@ public function testParseAttributeCompact() {
1932 1996
 		$this->assertEquals('', $helper->parseAttributes(array('require' => false)));
1933 1997
 	}
1934 1998
 
1935  
-}
  1999
+}
24  lib/Cake/View/Helper/HtmlHelper.php
@@ -682,6 +682,11 @@ public function getCrumbs($separator = '&raquo;', $startText = false) {
682 682
  * similar to HtmlHelper::getCrumbs(), so it uses options which every
683 683
  * crumb was added with.
684 684
  *
  685
+ * ### Options
  686
+ * - `separator` Separator content to insert in between breadcrumbs, defaults to ''
  687
+ * - `firstClass` Class for wrapper tag on the first breadcrumb, defaults to 'first'
  688
+ * - `lastClass` Class for wrapper tag on current active page, defaults to 'last'
  689
+ * 
685 690
  * @param array $options Array of html attributes to apply to the generated list elements.
686 691
  * @param string|array|boolean $startText This will be the first crumb, if false it defaults to first crumb in array. Can
687 692
  *   also be an array, see `HtmlHelper::getCrumbs` for details.
@@ -689,6 +694,12 @@ public function getCrumbs($separator = '&raquo;', $startText = false) {
689 694
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
690 695
  */
691 696
 	public function getCrumbList($options = array(), $startText = false) {
  697
+		$defaults = array('firstClass' => 'first', 'lastClass' => 'last', 'separator' => '');
  698
+		$options = array_merge($defaults, (array)$options);
  699
+		$firstClass = $options['firstClass'];
  700
+		$lastClass = $options['lastClass'];
  701
+		$separator = $options['separator'];
  702
+		unset($options['firstClass'], $options['lastClass'], $options['separator']);		
692 703
 		$crumbs = $this->_prepareCrumbs($startText);
693 704
 		if (!empty($crumbs)) {
694 705
 			$result = '';
@@ -702,9 +713,16 @@ public function getCrumbList($options = array(), $startText = false) {
702 713
 					$elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
703 714
 				}
704 715
 				if (!$which) {
705  
-					$options['class'] = 'first';
  716
+					if ($firstClass !== false) {
  717
+						$options['class'] = $firstClass;
  718
+					}
706 719
 				} elseif ($which == $crumbCount - 1) {
707  
-					$options['class'] = 'last';
  720
+					if ($lastClass !== false) {
  721
+						$options['class'] = $lastClass;
  722
+					}
  723
+				}
  724
+				if (!empty($separator) && ($crumbCount - $which >= 2)) {
  725
+					$elementContent .= $separator;
708 726
 				}
709 727
 				$result .= $this->tag('li', $elementContent, $options);
710 728
 			}
@@ -1211,4 +1229,4 @@ public function loadConfig($configFile, $path = null) {
1211 1229
 		return $configs;
1212 1230
 	}
1213 1231
 
1214  
-}
  1232
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.