From 6e63e531690a49a52d8d82922faae5ac624c6d05 Mon Sep 17 00:00:00 2001 From: Milos Levacic Date: Sat, 25 Oct 2014 19:14:47 +0200 Subject: [PATCH] Add ability to pass arbitrary data into breadcrumbs Closes #35 --- README.md | 19 +++++++++++++++++++ src/DaveJamesMiller/Breadcrumbs/Generator.php | 3 ++- tests/GeneratorTest.php | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c747084..cf70248 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,25 @@ The default Twitter Bootstrap templates provided render this with a CSS class of "active", the same as the last breadcrumb, because otherwise they default to black text not grey which doesn't look right. +### Breadcrumbs with custom data + +The `push()` method accepts an optional third parameter, `$options` - an array +of data to be passed to the breadcrumb, which you can later use in your custom +template. For example, if you wanted each breadcrumb to have an icon, you could +do: + +```php +$breadcrumbs->push('Home', '/', array('icon' => '/images/icons/home.png')); +``` + +The `$options` array will be added to the breadcrumb as an `options` property, +so you would access the icon as `$breadcrumb->options['icon']` in your template, +like this: + +``` +
  • {{{ $breadcrumb->title }}}
  • +``` + ### Defining breadcrumbs in a different file If you don't want to use `app/breadcrumbs.php`, you can define them in diff --git a/src/DaveJamesMiller/Breadcrumbs/Generator.php b/src/DaveJamesMiller/Breadcrumbs/Generator.php index 928488d..fbfaca0 100644 --- a/src/DaveJamesMiller/Breadcrumbs/Generator.php +++ b/src/DaveJamesMiller/Breadcrumbs/Generator.php @@ -46,11 +46,12 @@ public function parentArray($name, $params = array()) $this->call($name, $params); } - public function push($title, $url = null) + public function push($title, $url = null, array $options = array()) { $this->breadcrumbs[] = (object) array( 'title' => $title, 'url' => $url, + 'options' => $options, // These will be altered later where necessary: 'first' => false, 'last' => false, diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 11967b9..043a9ba 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -47,6 +47,7 @@ public function testPush() $this->assertSame('Home', $breadcrumbs[0]->title); $this->assertSame('/', $breadcrumbs[0]->url); + $this->assertSame(array(), $breadcrumbs[0]->options); } public function testPushWithoutUrl() @@ -61,6 +62,20 @@ public function testPushWithoutUrl() $this->assertNull($breadcrumbs[0]->url); } + public function testPushWithOptions() + { + $options = array( + 'foo' => 'bar', + 'baz' => 'qux', + ); + + $generator = new Generator(array()); + $generator->push('Home', '/', $options); + $breadcrumbs = $generator->get(); + + $this->assertSame($options, $breadcrumbs[0]->options); + } + public function testToArray() { $generator = new Generator(array());