RSS

How to create a breadcrumb widget

05 Jul

By:

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.

Info: Please note, that new versions of Yii Framework already contain a bulit-in breadcrumbs widget. See this class reference chapter for more information. Information in this article may be, however, usable to build custom one, not descending from bulit-in one.

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
)); ?>
Advertisements
 
Leave a comment

Posted by on July 5, 2012 in PHP, Yii

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: