Class yii\bootstrap5\NavBar

Inheritanceyii\bootstrap5\NavBar » yii\bootstrap5\Widget » yii\base\Widget
Uses Traitsyii\bootstrap5\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/NavBar.php

NavBar renders a navbar HTML component.

Any content enclosed between the begin() and end() calls of NavBar is treated as the content of the navbar. You may use widgets such as yii\bootstrap5\Nav or \yii\widgets\Menu to build up such content. For example,

use yii\bootstrap5\NavBar;
use yii\bootstrap5\Nav;

NavBar::begin(['brandLabel' => 'NavBar Test']);
echo Nav::widget([
    'items' => [
        ['label' => 'Home', 'url' => ['/site/index']],
        ['label' => 'About', 'url' => ['/site/about']],
    ],
    'options' => ['class' => 'navbar-nav'],
]);
NavBar::end();

See also https://getbootstrap.com/docs/5.1/components/navbar/.

Public Properties

Hide inherited properties

Property Type Description Defined By
$brandImage string|boolean Src of the brand image or false if it's not used. yii\bootstrap5\NavBar
$brandImageOptions array The HTML attributes of the brand image. yii\bootstrap5\NavBar
$brandLabel string|boolean The text of the brand or false if it's not used. yii\bootstrap5\NavBar
$brandOptions array The HTML attributes of the brand link. yii\bootstrap5\NavBar
$brandUrl array|string|boolean The URL for the brand's hyperlink tag. yii\bootstrap5\NavBar
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap5\BootstrapWidgetTrait
$clientOptions yii\bootstrap5\NavBar
$collapseOptions array|boolean The HTML attributes for the collapse container tag. yii\bootstrap5\NavBar
$containerOptions array yii\bootstrap5\NavBar
$innerContainerOptions array The HTML attributes of the inner container. yii\bootstrap5\NavBar
$offcanvasOptions array|boolean The HTML attributes for the offcanvas container tag. yii\bootstrap5\NavBar
$options array The HTML attributes for the widget container tag. yii\bootstrap5\NavBar
$renderInnerContainer boolean Whether the navbar content should be included in an inner div container which by default adds left and right padding. yii\bootstrap5\NavBar
$screenReaderToggleText string Text to show for screen readers for the button to toggle the navbar. yii\bootstrap5\NavBar
$togglerContent string The toggle button content. yii\bootstrap5\NavBar
$togglerOptions array The HTML attributes of the navbar toggler button. yii\bootstrap5\NavBar

Public Methods

Hide inherited methods

Method Description Defined By
init() yii\bootstrap5\NavBar
run() Renders the widget. yii\bootstrap5\NavBar
setContainerOptions() Container options setter for backwards compatibility yii\bootstrap5\NavBar

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap5\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin/component and the related events. yii\bootstrap5\BootstrapWidgetTrait
renderToggleButton() Renders collapsible toggle button. yii\bootstrap5\NavBar

Property Details

Hide inherited properties

$brandImage public property (available since version 2.0.8)

Src of the brand image or false if it's not used. Note that this param will override $this->brandLabel param.

See also https://getbootstrap.com/docs/5.1/components/navbar/.

public string|boolean $brandImage false
$brandImageOptions public property

The HTML attributes of the brand image.

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

$brandLabel public property

The text of the brand or false if it's not used. Note that this is not HTML-encoded.

See also https://getbootstrap.com/docs/5.1/components/navbar/.

public string|boolean $brandLabel false
$brandOptions public property

The HTML attributes of the brand link.

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

public array $brandOptions = []
$brandUrl public property

The URL for the brand's hyperlink tag. This parameter will be processed by \yii\helpers\Url::to() and will be used for the "href" attribute of the brand link. Default value is false that means \yii\web\Application::homeUrl will be used. You may set it to null if you want to have no link at all.

public array|string|boolean $brandUrl false
$clientOptions public property
public $clientOptions = []
$collapseOptions public property

The HTML attributes for the collapse container tag. The following special options are recognized:

  • tag: string, defaults to "div", the name of the container tag.

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

$containerOptions public property
public array $containerOptions null
$innerContainerOptions public property

The HTML attributes of the inner container.

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

$offcanvasOptions public property

The HTML attributes for the offcanvas container tag.

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

$options public property

The HTML attributes for the widget container tag. The following special options are recognized:

  • tag: string, defaults to "nav", the name of the container tag.

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

public array $options = []
$renderInnerContainer public property

Whether the navbar content should be included in an inner div container which by default adds left and right padding. Set this to false for a 100% width navbar.

$screenReaderToggleText public property

Text to show for screen readers for the button to toggle the navbar.

$togglerContent public property

The toggle button content. Defaults to bootstrap 5 default <span class="navbar-toggler-icon"></span>

public string $togglerContent '<span class="navbar-toggler-icon"></span>'
$togglerOptions public property

The HTML attributes of the navbar toggler button.

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

public array $togglerOptions = []

Method Details

Hide inherited methods

init() public method

public void init ( )

                public function init(): void
{
    parent::init();
    if (!isset($this->options['class']) || empty($this->options['class'])) {
        Html::addCssClass($this->options, [
            'widget' => 'navbar',
            'toggle' => 'navbar-expand-lg',
            'navbar-light',
            'bg-light',
        ]);
    } else {
        Html::addCssClass($this->options, [
            'widget' => 'navbar',
        ]);
    }
    $navOptions = $this->options;
    $navTag = ArrayHelper::remove($navOptions, 'tag', 'nav');
    $brand = '';
    if (!isset($this->innerContainerOptions['class'])) {
        Html::addCssClass($this->innerContainerOptions, [
            'panel' => 'container',
        ]);
    }
    if ($this->collapseOptions !== false && !isset($this->collapseOptions['id'])) {
        $this->collapseOptions['id'] = "{$this->options['id']}-collapse";
    } elseif ($this->offcanvasOptions !== false && !isset($this->offcanvasOptions['id'])) {
        $this->offcanvasOptions['id'] = "{$this->options['id']}-offcanvas";
    }
    if ($this->brandImage !== false) {
        $this->brandLabel = Html::img($this->brandImage, $this->brandImageOptions);
    }
    if ($this->brandLabel !== false) {
        Html::addCssClass($this->brandOptions, [
            'widget' => 'navbar-brand',
        ]);
        if ($this->brandUrl === null) {
            $brand = Html::tag('span', $this->brandLabel, $this->brandOptions);
        } else {
            $brand = Html::a(
                $this->brandLabel,
                $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl,
                $this->brandOptions,
            );
        }
    }
    ob_start();
    echo Html::beginTag($navTag, $navOptions) . "\n";
    if ($this->renderInnerContainer) {
        echo Html::beginTag('div', $this->innerContainerOptions) . "\n";
    }
    echo $brand . "\n";
    echo $this->renderToggleButton() . "\n";
    if ($this->collapseOptions !== false) {
        Html::addCssClass($this->collapseOptions, [
            'collapse' => 'collapse',
            'widget' => 'navbar-collapse',
        ]);
        $collapseOptions = $this->collapseOptions;
        $collapseTag = ArrayHelper::remove($collapseOptions, 'tag', 'div');
        echo Html::beginTag($collapseTag, $collapseOptions) . "\n";
    } elseif ($this->offcanvasOptions !== false) {
        Offcanvas::begin($this->offcanvasOptions);
    }
}

            
registerClientEvents() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerClientEvents()

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));
    }
}

            
registerPlugin() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerPlugin()

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);
    }
}

            
renderToggleButton() protected method

Renders collapsible toggle button.

protected string renderToggleButton ( )
return string

The rendering toggle button.

                protected function renderToggleButton(): string
{
    if ($this->collapseOptions === false && $this->offcanvasOptions === false) {
        return '';
    }
    $options = $this->togglerOptions;
    Html::addCssClass($options, [
        'widget' => 'navbar-toggler',
    ]);
    if ($this->offcanvasOptions !== false) {
        $bsData = [
            'bs-toggle' => 'offcanvas',
            'bs-target' => '#' . $this->offcanvasOptions['id'],
        ];
        $aria = $this->offcanvasOptions['id'];
    } elseif ($this->collapseOptions !== false) {
        $bsData = [
            'bs-toggle' => 'collapse',
            'bs-target' => '#' . $this->collapseOptions['id'],
        ];
        $aria = $this->collapseOptions['id'];
    }
    return Html::button(
        $this->togglerContent,
        ArrayHelper::merge($options, [
            'type' => 'button',
            'data' => $bsData,
            'aria' => [
                'controls' => $aria,
                'expanded' => 'false',
                'label' => $this->screenReaderToggleText ?: Yii::t('yii/bootstrap5', 'Toggle navigation'),
            ],
        ]),
    );
}

            
run() public method

Renders the widget.

public string run ( )

                public function run(): string
{
    if ($this->collapseOptions !== false) {
        $tag = ArrayHelper::remove($this->collapseOptions, 'tag', 'div');
        echo Html::endTag($tag) . "\n";
    } elseif ($this->offcanvasOptions !== false) {
        Offcanvas::end();
    }
    $content = ob_get_clean();
    if ($this->renderInnerContainer) {
        $content .= Html::endTag('div') . "\n";
    }
    $tag = ArrayHelper::remove($this->options, 'tag', 'nav');
    $content .= Html::endTag($tag);
    BootstrapPluginAsset::register($this->getView());
    return $content;
}

            
setContainerOptions() public method

Container options setter for backwards compatibility

public void setContainerOptions ( array $collapseOptions )
$collapseOptions array

                public function setContainerOptions(array $collapseOptions): void
{
    $this->collapseOptions = $collapseOptions;
}