How to create a breadcrumb widget
Here's a simple way to create a breadcrumb widget to be used in your templates. The idea is just to isolate how the breadcrumb is generated based on an array of crumbs.
components/BreadCrumb.php:
class BreadCrumb extends CWidget { public $crumbs = array(); public $delimiter = ' / '; public function run() { $this->render('breadCrumb'); } }
components/views/breadCrumb.php:
<div id="breadCrumb"> <?php foreach($this->crumbs as $crumb) { if(isset($crumb['url'])) { echo CHtml::link($crumb['name'], $crumb['url']); } else { echo $crumb['name']; } if(next($this->crumbs)) { echo $this->delimiter; } } </div>
Usage in views
$this->widget('application.components.BreadCrumb', array( 'crumbs' => array( array('name' => 'Home', 'url' => array('site/index')), array('name' => 'Login'), ), 'delimiter' => ' → ', // if you want to change it ));
Links
Total 5 comments:
#568
Auto Breadcrumb Widget
by
nickelstar at 8:12pm on August 11, 2009.
In an attempt to expand on this example, I created a breadcrumb widget that automatically manages some features. Please take a look here for details:
http://www.yiiframework.com/forum/index.php?/topic/3741-auto-breadcrumbs/
#1254
note
by
napeHeK at 3:41am on March 10, 2010.
<?php class BreadCrumb extends CWidget { public $crumbs = array(); public $delimiter = ' ยป ';
public function run()
{
foreach($this->crumbs as $key => $crumb)
{
if(isset($crumb['url']))
$this->crumbs[$key] = CHtml::link($crumb['name'], $crumb['url']);
else
$this->crumbs[$key] = $crumb['name'];
}
$crumbs = implode($this->delimiter, $this->crumbs);
$this->render('breadCrumb', array('crumbs' => $crumbs));
}
}
#1405
New Method
by
francis at 4:17am on April 20, 2010.
<?php
$this->pageTitle=Yii::app()->name . ' - Register';
$this->breadcrumbs=array(
'Home' => '/site/home',
'Register' => '/site/register,
);
?>
#1406
Note
by
francis at 4:19am on April 20, 2010.
$this->breadcrumbs=array(
'Home' => '/site/home',
'Login' => '/site/login',
);
I have no idea that why the example not work but not mine.

I'm hesitant to change this myself as I did not put work into writing this article but I prefer this shorthand way of defining crumbs:
eg no array keys defined.