How to create a breadcrumb widget

You are viewing revision #4 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#3)next (#5) »

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

<?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

10 0
11 followers
Viewed: 84 760 times
Version: Unknown (update)
Category: Tutorials
Tags:
Written by: knut
Last updated by: Yang He
Created on: Mar 19, 2009
Last updated: 12 years ago
Update Article

Revisions

View all history