CBreadcrumbs.php
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* CBreadcrumbs class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* 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 {@link links} property, which specifies
* the links to be displayed. For example,
*
* <pre>
* $this->widget('zii.widgets.CBreadcrumbs', array(
* 'links'=>array(
* 'Sample post'=>array('post/view', 'id'=>12),
* 'Edit',
* ),
* ));
* </pre>
*
* 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:
*
* <pre>
* $this->widget('zii.widgets.CBreadcrumbs', array(
* 'links'=>$this->breadcrumbs,
* ));
* </pre>
*
* Then, in each view script, one only needs to assign the "breadcrumbs" property as needed.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package zii.widgets
* @since 1.1
*/
class CBreadcrumbs extends CWidget
{
/**
* @var string the tag name for the breadcrumbs container tag. Defaults to 'div'.
*/
public $tagName='div';
/**
* @var array the HTML attributes for the breadcrumbs container tag.
*/
public $htmlOptions=array('class'=>'breadcrumbs');
/**
* @var boolean whether to HTML encode the link labels. Defaults to true.
*/
public $encodeLabel=true;
/**
* @var string the first hyperlink in the breadcrumbs (called home link).
* If this property is not set, it defaults to a link pointing to {@link CWebApplication::homeUrl} with label 'Home'.
* If this property is false, the home link will not be rendered.
*/
public $homeLink;
/**
* @var array 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 {@link 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 {@link homeLink} separately.
*
* <pre>
* array(
* 'Sample post'=>array('post/view', 'id'=>12),
* 'Edit',
* )
* </pre>
*/
public $links=array();
/**
* @var string String, specifies how each active item is rendered. Defaults to
* "<a href="{url}">{label}</a>", where "{label}" will be replaced by the corresponding item
* label while "{url}" will be replaced by the URL of the item.
* @since 1.1.11
*/
public $activeLinkTemplate='<a href="{url}">{label}</a>';
/**
* @var string String, specifies how each inactive item is rendered. Defaults to
* "<span>{label}</span>", where "{label}" will be replaced by the corresponding item label.
* Note that inactive template does not have "{url}" parameter.
* @since 1.1.11
*/
public $inactiveLinkTemplate='<span>{label}</span>';
/**
* @var string the separator between links in the breadcrumbs. Defaults to ' » '.
*/
public $separator=' » ';
/**
* Renders the content of the portlet.
*/
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);
else if($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);
}
}