Breadcrumbs.php 4.69 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
Alexander Makarov committed
26 27 28
 * echo Breadcrumbs::widget([
 *     'links' => [
 *         ['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]],
29
 *         'Edit',
Alexander Makarov committed
30 31
 *     ],
 * ]);
32 33
 * ~~~
 *
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
Alexander Makarov committed
40 41 42
 * echo Breadcrumbs::widget([
 *     'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
 * ]);
43
 * ~~~
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
	public $tag = 'ul';
	/**
	 * @var array the HTML attributes for the breadcrumb container tag.
	 */
Alexander Makarov committed
57
	public $options = ['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
	 * ~~~
Alexander Makarov committed
74
	 * [
75 76
	 *     'label' => 'label of the link',  // required
	 *     'url' => 'url of the link',      // optional, will be processed by Html::url()
Alexander Makarov committed
77
	 * ]
78 79
	 * ~~~
	 *
Alexander Makarov committed
80
	 * If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`,
81
	 * you should simply use `$label`.
Qiang Xue committed
82
	 */
Alexander Makarov committed
83
	public $links = [];
Qiang Xue committed
84
	/**
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
			return;
		}
Alexander Makarov committed
103
		$links = [];
Qiang Xue committed
104
		if ($this->homeLink === null) {
Alexander Makarov committed
105
			$links[] = $this->renderItem([
106
				'label' => Yii::t('yii', 'Home'),
Qiang Xue committed
107
				'url' => Yii::$app->homeUrl,
Alexander Makarov committed
108
			], $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
			if (!is_array($link)) {
Alexander Makarov committed
114
				$link = ['label' => $link];
115
			}
Qiang Xue committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			$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'])) {
Alexander Makarov committed
136
			return strtr($template, ['{link}' => Html::a($label, $link['url'])]);
Qiang Xue committed
137
		} else {
Alexander Makarov committed
138
			return strtr($template, ['{link}' => $label]);
Qiang Xue committed
139 140
		}
	}
Qiang Xue committed
141
}