Class yii\bootstrap5\Breadcrumbs
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Source Code | https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/Breadcrumbs.php |
This widget represents a Bootstrap 5 component "Breadcrumb". It displays a list of links indicating the position of the current page in the whole site hierarchy.
echo Breadcrumbs::widget([
'links' => [
[
'label' => 'the item label', // required
'url' => 'the item URL', // optional, will be processed by `Url::to()`
'template' => 'own template of the item', // optional
],
['label' => 'the label of the active item']
],
'options' => [...],
]);
or
`php
echo Breadcrumbs::widget([
'links' => [
'the item URL' => 'the item label',
0 => 'the label of the active item',
],
'options' => [...],
]);
`
See also https://getbootstrap.com/docs/5.1/components/breadcrumb/.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $activeItemTemplate | yii\ |
||
| $clientEvents | array | The event handlers for the underlying Bootstrap JS plugin. | yii\ |
| $clientOptions | array|false | The options for the underlying Bootstrap JS plugin/component. | yii\ |
| $homeLink | array|false | The first hyperlink in the breadcrumbs (called home link). | yii\ |
| $itemTemplate | yii\ |
||
| $navOptions | array | The HTML attributes for the widgets nav container tag. | yii\ |
| $tag | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| activeItemTemplate() | 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. |
yii\ |
| encodeLabels() | Whether to HTML-encode the link labels. | yii\ |
| homeLink() | The first hyperlink in the breadcrumbs (called home link). | yii\ |
| init() | Initializes the widget. | yii\ |
| itemTemplate() | 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. |
yii\ |
| links() | List of links to appear in the breadcrumbs. If this property is empty, the widget will not render anything. | yii\ |
| navOptions() | The HTML attributes for the widgets nav container tag. | yii\ |
| options() | The HTML attributes for the widget container tag. The following special options are recognized. | yii\ |
| run() | yii\ |
|
| tag() | The name of the breadcrumb container tag. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| registerClientEvents() | Registers JS event handlers that are listed in $clientEvents. | yii\ |
| registerPlugin() | Registers a specific Bootstrap plugin/component and the related events. | yii\ |
Property Details
The first hyperlink in the breadcrumbs (called home link).
Please refer to links() on the format of the link.
If this property is not set, it will default to a link pointing to \
Method Details
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.
| public $this activeItemTemplate ( string $value ) | ||
| $value | string | |
public function activeItemTemplate(string $value): self
{
$this->activeItemTemplate = $value;
return $this;
}
Whether to HTML-encode the link labels.
| public $this encodeLabels ( boolean $value ) | ||
| $value | boolean | |
public function encodeLabels(bool $value): self
{
$this->encodeLabels = $value;
return $this;
}
The first hyperlink in the breadcrumbs (called home link).
Please refer to {@see \
If this property is not set, it will default to a link pointing with the label 'Home'. If this property is false, the home link will not be rendered.
| public $this homeLink ( array|false $value ) | ||
| $value | array|false | |
public function homeLink($value): self
{
$this->homeLink = $value;
return $this;
}
Defined in:
yii\
Initializes the widget.
This method will register the bootstrap asset bundle. If you override this method, make sure you call the parent implementation first.
| public void init ( ) | ||
| throws | \ |
|
|---|---|---|
public function init(): void
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
}
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.
| public $this itemTemplate ( string $value ) | ||
| $value | string | |
public function itemTemplate(string $value): self
{
$this->itemTemplate = $value;
return $this;
}
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 item in the breadcrumbs with the following structure.
| public $this links ( array $value ) | ||
| $value | array | |
public function links(array $value): self
{
$this->links = $value;
return $this;
}
The HTML attributes for the widget container tag. The following special options are recognized.
{@see \
| public $this options ( array $value ) | ||
| $value | array | |
public function options(array $value): self
{
$this->options = $value;
return $this;
}
Defined in:
yii\
Registers JS event handlers that are listed in $clientEvents.
| protected void registerClientEvents ( ?string $name = null ) | ||
| $name | ?string | |
protected function registerClientEvents(?string $name = null): void
{
if (!empty($this->clientEvents)) {
$id = $this->options['id'];
$js = [];
$appendix = ($name === 'dropdown') ? '.parentElement' : '';
foreach ($this->clientEvents as $event => $handler) {
$js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
}
$this->getView()->registerJs(implode("\n" , $js));
}
}
Defined in:
yii\
Registers a specific Bootstrap plugin/component and the related events.
| protected void registerPlugin ( string $name ) | ||
| $name | string |
The name of the Bootstrap plugin |
protected function registerPlugin(string $name): void
{
/**
* @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
*/
$jsPlugins = [
'alert',
'button',
'carousel',
'collapse',
'dropdown',
'modal',
'offcanvas',
'popover',
'scrollspy',
'tab',
'toast',
'tooltip',
];
if (in_array($name, $jsPlugins, true)) {
$view = $this->getView();
BootstrapPluginAsset::register($view);
// 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
$name = ucfirst($name);
$id = $this->options['id'];
$options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
$view->registerJs("(new bootstrap.$name('#$id', $options));");
}
$this->registerClientEvents($name);
}
}
| public string run ( ) |
public function run(): string
{
if (empty($this->links)) {
return '';
}
// Normalize links
$links = [];
foreach ($this->links as $key => $value) {
if (is_array($value)) {
$links[] = $value;
} else {
$links[] = [
'label' => $value,
'url' => is_string($key) ? $key : null,
];
}
}
$this->links = $links;
unset($links);
if ($this->homeLink === []) {
$this->homeLink = null;
}
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-breadcrumb";
}
Html::addCssClass($this->options, [
'widget' => 'breadcrumb',
]);
// parent method not return result
ob_start();
parent::run();
$content = ob_get_clean();
return Html::tag('nav', $content, $this->navOptions);
}