Class hscstudio\heart\widgets\Breadcrumbs

Inheritancehscstudio\heart\widgets\Breadcrumbs » yii\base\Widget
Available since version2.0

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.

To use Breadcrumbs, you need to configure its $links property, which specifies the links to be displayed. For example,

// $this is the view object currently being used
echo Breadcrumbs::widget([
    'itemTemplate' => "<li><i>{link}</i></li>\n", // template for all links
    'links' => [
        [
            'label' => 'Post Category',
            'url' => ['post-category/view', 'id' => 10],
            'template' => '<li><b>{link}</b></li>\n', // template for this link only
        ],
        ['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]],
        'Edit',
    ],
]);

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 views. In the layout view, you assign this view parameter to the $links property like the following:

// $this is the view object currently being used
echo Breadcrumbs::widget([
    'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]);

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$activeItemTemplate string The template used to render each active item in the breadcrumbs. hscstudio\heart\widgets\Breadcrumbs
$encodeLabels boolean Whether to HTML-encode the link labels. hscstudio\heart\widgets\Breadcrumbs
$itemTemplate string The template used to render each inactive item in the breadcrumbs. hscstudio\heart\widgets\Breadcrumbs
$options array The HTML attributes for the breadcrumb container tag. hscstudio\heart\widgets\Breadcrumbs
$tag string The name of the breadcrumb container tag. hscstudio\heart\widgets\Breadcrumbs

Public Methods

Hide inherited methods

MethodDescriptionDefined By
run() Renders the widget. hscstudio\heart\widgets\Breadcrumbs

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
renderItem() Renders a single breadcrumb item. hscstudio\heart\widgets\Breadcrumbs

Property Details

$activeItemTemplate public property
string $activeItemTemplate "<li class=\"active\">{link}</li>\n"

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.

$encodeLabels public property

Whether to HTML-encode the link labels.

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 \yii\web\Application::homeUrl with the label 'Home'. If this property is false, the home link will not be rendered.

$itemTemplate public property
string $itemTemplate "<li>{link}</li>\n"

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.

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:

[
    'label' => 'label of the link',  // required
    'url' => 'url of the link',      // optional, will be processed by Url::to()
    'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used
]

If a link is active, you only need to specify its "label", and instead of writing ['label' => $label], you should simply use $label.

$options public property
array $options = ['class' => 'breadcrumb']

The HTML attributes for the breadcrumb container tag.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

$tag public property
string $tag 'ul'

The name of the breadcrumb container tag.

Method Details

renderItem() protected method

Renders a single breadcrumb item.

string renderItem$link$template )
$link array

The link to be rendered. It must contain the "label" element. The "url" element is optional.

$template string

The template to be used to rendered the link. The token "{link}" will be replaced by the link.

return string

The rendering result

throws \yii\base\InvalidConfigException

if $link does not have "label" element.

run() public method

Renders the widget.

void run( )