Difference between #4 and #3 of How to create a breadcrumb widget

unchanged
Title
How to create a breadcrumb widget
unchanged
Category
Tutorials
unchanged
Tags
changed
Content
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:**
~~~
[php]
<?php
class BreadCrumb extends CWidget {
	
	public $crumbs = array();
	public $delimiter = ' / ';
	
	public function run() {
		$this->render('breadCrumb');
	}

}
?>
~~~

**components/views/breadCrumb.php:**
~~~
[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
~~~
[php]
<?php $this->widget('application.components.BreadCrumb', array(
  'crumbs' => array(
    array('name' => 'Home', 'url' => array('site/index')),
    array('name' => 'Login'),
  ),
  'delimiter' => ' &rarr; ', // if you want to change it
)); ?>
~~~

### Links
[Chinese
version](http://dreamneverfall.cn/node/107)version](http://projects.ourplanet.tk/node/107)
Write new article
  • Written by: knut
  • Updated by: Yang He
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +12
  • Viewed: 57,418 times
  • Created on: Mar 19, 2009
  • Last updated: Jun 29, 2012