Skip to content

Commit

Permalink
Add ability to pass arbitrary data into breadcrumbs
Browse files Browse the repository at this point in the history
Closes d13r#35
  • Loading branch information
levacic committed Oct 25, 2014
1 parent a658ae7 commit 6e63e53
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
<li><a href="{{{ $breadcrumb->url }}}"><img src="{{{ $breadcrumb->options['icon'] }}}">{{{ $breadcrumb->title }}}</a></li>
```

### Defining breadcrumbs in a different file

If you don't want to use `app/breadcrumbs.php`, you can define them in
Expand Down
3 changes: 2 additions & 1 deletion src/DaveJamesMiller/Breadcrumbs/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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());
Expand Down

0 comments on commit 6e63e53

Please sign in to comment.