Class yii\bootstrap\NavBar

Inheritanceyii\bootstrap\NavBar » yii\bootstrap\Widget » yii\base\Widget
Uses Traitsyii\bootstrap\BootstrapWidgetTrait
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-bootstrap/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\bootstrap\Nav or \yii\widgets\Menu to build up such content. For example,

use yii\bootstrap\NavBar;
use yii\bootstrap\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/3.3/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\bootstrap\NavBar
$brandLabel string|boolean The text of the brand or false if it's not used. yii\bootstrap\NavBar
$brandOptions array The HTML attributes of the brand link. yii\bootstrap\NavBar
$brandUrl array|string|boolean The URL for the brand's hyperlink tag. yii\bootstrap\NavBar
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap\BootstrapWidgetTrait
$clientOptions array The options for the underlying Bootstrap JS plugin. yii\bootstrap\BootstrapWidgetTrait
$containerOptions array The HTML attributes for the container tag. yii\bootstrap\NavBar
$headerContent string HTML content to be added in navbar-header div, for example, mobile search form. yii\bootstrap\NavBar
$innerContainerOptions array The HTML attributes of the inner container. yii\bootstrap\NavBar
$options array The HTML attributes for the widget container tag. yii\bootstrap\NavBar
$renderInnerContainer boolean Whether the navbar content should be included in an inner div container which by default adds left and right padding. yii\bootstrap\NavBar
$screenReaderToggleText string Text to show for screen readers for the button to toggle the navbar. yii\bootstrap\NavBar

Public Methods

Hide inherited methods

Method Description Defined By
getView() yii\bootstrap\BootstrapWidgetTrait
init() Initializes the widget. yii\bootstrap\NavBar
run() Renders the widget. yii\bootstrap\NavBar

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin and the related events yii\bootstrap\BootstrapWidgetTrait
renderToggleButton() Renders collapsible toggle button. yii\bootstrap\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/3.3/components/#navbar.

public string|boolean $brandImage false
$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/3.3/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
$containerOptions public property

The HTML attributes for the 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.

$headerContent public property (available since version 2.0.8)

HTML content to be added in navbar-header div, for example, mobile search form.

public string $headerContent 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.

$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.

public string $screenReaderToggleText 'Toggle navigation'

Method Details

Hide inherited methods

getView() public abstract method

Defined in: yii\bootstrap\BootstrapWidgetTrait::getView()

See also \yii\base\Widget::getView().

public abstract \yii\web\View getView ( )
return \yii\web\View

The view object that can be used to render views or view files.

                abstract function getView();

            
init() public method

Initializes the widget.

public void init ( )

                public function init()
{
    parent::init();
    $this->clientOptions = false;
    if (empty($this->options['class'])) {
        Html::addCssClass($this->options, ['navbar', 'navbar-default']);
    } else {
        Html::addCssClass($this->options, ['widget' => 'navbar']);
    }
    $options = $this->options;
    $tag = ArrayHelper::remove($options, 'tag', 'nav');
    echo Html::beginTag($tag, $options);
    if ($this->renderInnerContainer) {
        if (!isset($this->innerContainerOptions['class'])) {
            Html::addCssClass($this->innerContainerOptions, 'container');
        }
        echo Html::beginTag('div', $this->innerContainerOptions);
    }
    echo Html::beginTag('div', ['class' => 'navbar-header']);
    if (!isset($this->containerOptions['id'])) {
        $this->containerOptions['id'] = "{$this->options['id']}-collapse";
    }
    echo $this->renderToggleButton();
    if ($this->brandImage !== false) {
        $this->brandLabel = Html::img($this->brandImage);
    }
    if ($this->brandLabel !== false) {
        Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']);
        echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
    }
    echo $this->headerContent;
    echo Html::endTag('div');
    Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']);
    $options = $this->containerOptions;
    $tag = ArrayHelper::remove($options, 'tag', 'div');
    echo Html::beginTag($tag, $options);
}

            
registerClientEvents() protected method (available since version 2.0.2)

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

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( )

                protected function registerClientEvents()
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "jQuery('#$id').on('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

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

Registers a specific Bootstrap plugin and the related events

protected void registerPlugin ( $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin($name)
{
    $view = $this->getView();
    BootstrapPluginAsset::register($view);
    $id = $this->options['id'];
    if ($this->clientOptions !== false) {
        $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
        $js = "jQuery('#$id').$name($options);";
        $view->registerJs($js);
    }
    $this->registerClientEvents();
}

            
renderToggleButton() protected method

Renders collapsible toggle button.

protected string renderToggleButton ( )
return string

The rendering toggle button.

                protected function renderToggleButton()
{
    $bar = Html::tag('span', '', ['class' => 'icon-bar']);
    $screenReader = "<span class=\"sr-only\">{$this->screenReaderToggleText}</span>";
    return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
        'class' => 'navbar-toggle',
        'data-toggle' => 'collapse',
        'data-target' => "#{$this->containerOptions['id']}",
    ]);
}

            
run() public method

Renders the widget.

public void run ( )

                public function run()
{
    $tag = ArrayHelper::remove($this->containerOptions, 'tag', 'div');
    echo Html::endTag($tag);
    if ($this->renderInnerContainer) {
        echo Html::endTag('div');
    }
    $tag = ArrayHelper::remove($this->options, 'tag', 'nav');
    echo Html::endTag($tag);
    BootstrapPluginAsset::register($this->getView());
}