Versions
Look up a class, method, property or event

CBreadcrumbs

Package zii.widgets
Inheritance class CBreadcrumbs » CWidget » CBaseController » CComponent
Since 1.1
Source Code framework/zii/widgets/CBreadcrumbs.php
CBreadcrumbs displays a list of links indicating the position of the current page in the whole website.

For example, breadcrumbs like "Home > Sample Post > Edit" means the user is viewing an edit page for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home" to return to the homepage.

To use CBreadcrumbs, one usually needs to configure its links property, which specifies the links to be displayed. For example,

$this->widget('zii.widgets.CBreadcrumbs', array(
    'links'=>array(
        'Sample post'=>array('post/view', 'id'=>12),
        'Edit',
    ),
));


Because breadcrumbs usually appears in nearly every page of a website, the widget is better to be placed in a layout view. One can define a property "breadcrumbs" in the base controller class and assign it to the widget in the layout, like the following:

$this->widget('zii.widgets.CBreadcrumbs', array(
    'links'=>$this->breadcrumbs,
));


Then, in each view script, one only needs to assign the "breadcrumbs" property as needed.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
actionPrefix string the prefix to the IDs of the actions. CWidget
activeLinkTemplate string String, specifies how each active item is rendered. CBreadcrumbs
controller CController Returns the controller that this widget belongs to. CWidget
encodeLabel boolean whether to HTML encode the link labels. CBreadcrumbs
htmlOptions array the HTML attributes for the breadcrumbs container tag. CBreadcrumbs
id string Returns the ID of the widget or generates a new one if requested. CWidget
inactiveLinkTemplate string String, specifies how each inactive item is rendered. CBreadcrumbs
owner CBaseController Returns the owner/creator of this widget. CWidget
separator string the separator between links in the breadcrumbs. CBreadcrumbs
skin mixed the name of the skin to be used by this widget. CWidget
tagName string the tag name for the breadcrumbs container tag. CBreadcrumbs
viewPath string Returns the directory containing the view files for this widget. CWidget

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CWidget
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
actions() Returns a list of actions that are used by this widget. CWidget
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
beginCache() Begins fragment caching. CBaseController
beginClip() Begins recording a clip. CBaseController
beginContent() Begins the rendering of content that is to be decorated by the specified view. CBaseController
beginWidget() Creates a widget and executes it. CBaseController
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
endCache() Ends fragment caching. CBaseController
endClip() Ends recording a clip. CBaseController
endContent() Ends the rendering of content. CBaseController
endWidget() Ends the execution of the named widget. CBaseController
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getController() Returns the controller that this widget belongs to. CWidget
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns the ID of the widget or generates a new one if requested. CWidget
getOwner() Returns the owner/creator of this widget. CWidget
getViewFile() Looks for the view script file according to the view name. CWidget
getViewPath() Returns the directory containing the view files for this widget. CWidget
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the widget. CWidget
raiseEvent() Raises an event. CComponent
render() Renders a view. CWidget
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
run() Renders the content of the portlet. CBreadcrumbs
setId() Sets the ID of the widget. CWidget
widget() Creates a widget and executes it. CBaseController

Property Details

activeLinkTemplate property (available since v1.1.11)
public string $activeLinkTemplate;

String, specifies how each active item is rendered. Defaults to "{label}", where "{label}" will be replaced by the corresponding item label while "{url}" will be replaced by the URL of the item.

encodeLabel property
public boolean $encodeLabel;

whether to HTML encode the link labels. Defaults to true.

public string $homeLink;

the first hyperlink in the breadcrumbs (called home link). If this property is not set, it defaults to a link pointing to CWebApplication::homeUrl with label 'Home'. If this property is false, the home link will not be rendered.

htmlOptions property
public array $htmlOptions;

the HTML attributes for the breadcrumbs container tag.

inactiveLinkTemplate property (available since v1.1.11)
public string $inactiveLinkTemplate;

String, specifies how each inactive item is rendered. Defaults to "{label}", where "{label}" will be replaced by the corresponding item label. Note that inactive template does not have "{url}" parameter.

public array $links;

list of hyperlinks to appear in the breadcrumbs. If this property is empty, the widget will not render anything. Each key-value pair in the array will be used to generate a hyperlink by calling CHtml::link(key, value). For this reason, the key refers to the label of the link while the value can be a string or an array (used to create a URL). For more details, please refer to CHtml::link. If an element's key is an integer, it means the element will be rendered as a label only (meaning the current page).

The following example will generate breadcrumbs as "Home > Sample post > Edit", where "Home" points to the homepage, "Sample post" points to the "index.php?r=post/view&id=12" page, and "Edit" is a label. Note that the "Home" link is specified via homeLink separately.

array(
    'Sample post'=>array('post/view', 'id'=>12),
    'Edit',
)

separator property
public string $separator;

the separator between links in the breadcrumbs. Defaults to ' » '.

tagName property
public string $tagName;

the tag name for the breadcrumbs container tag. Defaults to 'div'.

Method Details

run() method
public void run()
Source Code: framework/zii/widgets/CBreadcrumbs.php#108 (show)
public function run()
{
    if(empty(
$this->links))
        return;

    echo 
CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
    
$links=array();
    if(
$this->homeLink===null)
        
$links[]=CHtml::link(Yii::t('zii','Home'),Yii::app()->homeUrl);
    elseif(
$this->homeLink!==false)
        
$links[]=$this->homeLink;
    foreach(
$this->links as $label=>$url)
    {
        if(
is_string($label) || is_array($url))
            
$links[]=strtr($this->activeLinkTemplate,array(
                
'{url}'=>CHtml::normalizeUrl($url),
                
'{label}'=>$this->encodeLabel CHtml::encode($label) : $label,
            ));
        else
            
$links[]=str_replace('{label}',$this->encodeLabel CHtml::encode($url) : $url,$this->inactiveLinkTemplate);
    }
    echo 
implode($this->separator,$links);
    echo 
CHtml::closeTag($this->tagName);
}

Renders the content of the portlet.

Total 3 comments

#4214 report it
lgespee at 2011/06/16 09:49am
homeLink in HTML instead of array

As PrplHaz4 commented, the homeLink attribute should be a HTML link.

 
I find it very strange that all breadcrumb links can be specified using an array key/value pair, while the homeLink must be in HTML.

 
The description of the links attribute actually states the following about how the breadcrumb hyperlinks are created:

Each key-value pair in the array will be used to generate a hyperlink by calling CHtml::link(key, value).

 
I thinks setting the homeLink attribute, like the breadcrumb links are set, would be better/more logical:

'homeLink'=>array('Home'=>array('site/index'))
 
// the last argument is an array, because you might want to specify route and params, see the next example
'homeLink'=>array('Home'=>array('site/index', 'lang'=>$model->language))

As a temporary workaround the following might be used:

'homeLink'=>CHtml::link('Home', array('site/index'))
 
// or with action params
'homeLink'=>CHtml::link('Home', array('site/index', 'lang'=>$model->language))
#3468 report it
balrok at 2011/04/14 07:12am
homelink

I also had a problem with homelink but PrplHaz4's way is bad use: 'homeLink'=>CHtml::link('My home', '/'),

#72 report it
PrplHaz4 at 2010/09/23 03:56am
homeLink must be in HTML anchor format

To set the homeLink attribute, use

'homeLink'=>'<a href="/rs/index.php?r=Models/index">My Models</a>',

Leave a comment

Please to leave your comment.