Breadcrumbs.php 4.76 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\widgets;
Qiang Xue committed
9 10 11

use Yii;
use yii\base\Widget;
12
use yii\base\InvalidConfigException;
Qiang Xue committed
13 14 15
use yii\helpers\Html;

/**
16 17 18 19 20 21
 * Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy.
 *
 * 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.
 *
22
 * To use Breadcrumbs, you need to configure its [[links]] property, which specifies the links to be displayed. For example,
23 24
 *
 * ~~~
25
 * // $this is the view object currently being used
26
 * echo Breadcrumbs::widget(array(
27 28 29 30 31 32 33
 *     'links' => array(
 *         array('label' => 'Sample Post', 'url' => array('post/edit', 'id' => 1)),
 *         'Edit',
 *     ),
 * ));
 * ~~~
 *
34 35
 * Because breadcrumbs usually appears in nearly every page of a website, you may consider placing it in a layout view.
 * You can use a view parameter (e.g. `$this->params['breadcrumbs']`) to configure the links in different
36 37 38
 * views. In the layout view, you assign this view parameter to the [[links]] property like the following:
 *
 * ~~~
39
 * // $this is the view object currently being used
40
 * echo Breadcrumbs::widget(array(
41 42 43
 *     'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : array(),
 * ));
 * ~~~
Qiang Xue committed
44 45 46 47 48 49 50
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Breadcrumbs extends Widget
{
	/**
Qiang Xue committed
51
	 * @var string the name of the breadcrumb container tag.
Qiang Xue committed
52
	 */
Qiang Xue committed
53 54 55 56 57
	public $tag = 'ul';
	/**
	 * @var array the HTML attributes for the breadcrumb container tag.
	 */
	public $options = array('class' => 'breadcrumb');
Qiang Xue committed
58
	/**
59
	 * @var boolean whether to HTML-encode the link labels.
Qiang Xue committed
60 61 62 63
	 */
	public $encodeLabels = true;
	/**
	 * @var string the first hyperlink in the breadcrumbs (called home link).
64 65
	 * If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]]
	 * with the label 'Home'. If this property is false, the home link will not be rendered.
Qiang Xue committed
66 67 68
	 */
	public $homeLink;
	/**
69 70 71
	 * @var array list of links to appear in the breadcrumbs. If this property is empty,
	 * the widget will not render anything. Each array element represents a single link in the breadcrumbs
	 * with the following structure:
Qiang Xue committed
72
	 *
73
	 * ~~~
Qiang Xue committed
74
	 * array(
75 76
	 *     'label' => 'label of the link',  // required
	 *     'url' => 'url of the link',      // optional, will be processed by Html::url()
Qiang Xue committed
77
	 * )
78 79 80 81
	 * ~~~
	 *
	 * If a link is active, you only need to specify its "label", and instead of writing `array('label' => $label)`,
	 * you should simply use `$label`.
Qiang Xue committed
82 83 84
	 */
	public $links = array();
	/**
85 86
	 * @var string the template used to render each inactive item in the breadcrumbs. The token `{link}`
	 * will be replaced with the actual HTML link for each inactive item.
Qiang Xue committed
87
	 */
88
	public $itemTemplate = "<li>{link}</li>\n";
Qiang Xue committed
89
	/**
90 91
	 * @var string the template used to render each active item in the breadcrumbs. The token `{link}`
	 * will be replaced with the actual HTML link for each active item.
Qiang Xue committed
92 93 94 95
	 */
	public $activeItemTemplate = "<li class=\"active\">{link}</li>\n";

	/**
96
	 * Renders the widget.
Qiang Xue committed
97 98 99
	 */
	public function run()
	{
100
		if (empty($this->links)) {
Qiang Xue committed
101 102 103 104
			return;
		}
		$links = array();
		if ($this->homeLink === null) {
Qiang Xue committed
105
			$links[] = $this->renderItem(array(
106
				'label' => Yii::t('yii', 'Home'),
Qiang Xue committed
107 108
				'url' => Yii::$app->homeUrl,
			), $this->itemTemplate);
Qiang Xue committed
109
		} elseif ($this->homeLink !== false) {
Qiang Xue committed
110
			$links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
Qiang Xue committed
111 112
		}
		foreach ($this->links as $link) {
113 114 115
			if (!is_array($link)) {
				$link = array('label' => $link);
			}
Qiang Xue committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
			$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate);
		}
		echo Html::tag($this->tag, implode('', $links), $this->options);
	}

	/**
	 * Renders a single breadcrumb item.
	 * @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional.
	 * @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link.
	 * @return string the rendering result
	 * @throws InvalidConfigException if `$link` does not have "label" element.
	 */
	protected function renderItem($link, $template)
	{
		if (isset($link['label'])) {
			$label = $this->encodeLabels ? Html::encode($link['label']) : $link['label'];
		} else {
			throw new InvalidConfigException('The "label" element is required for each link.');
		}
		if (isset($link['url'])) {
			return strtr($template, array('{link}' => Html::a($label, $link['url'])));
		} else {
			return strtr($template, array('{link}' => $label));
Qiang Xue committed
139 140
		}
	}
Qiang Xue committed
141
}