NavBar.php 3.41 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

10
use Yii;
11 12 13 14 15
use yii\helpers\Html;

/**
 * NavBar renders a navbar HTML component.
 *
16 17 18
 * 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 [[Nav]]
 * or [[\yii\widgets\Menu]] to build up such content. For example,
19 20
 *
 * ```php
21 22 23
 * use yii\bootstrap\NavBar;
 * use yii\widgets\Menu;
 *
Alexander Makarov committed
24 25 26 27 28 29 30
 * NavBar::begin(['brandLabel' => 'NavBar Test']);
 * echo Nav::widget([
 *     'items' => [
 *         ['label' => 'Home', 'url' => ['/site/index']],
 *         ['label' => 'About', 'url' => ['/site/about']],
 *     ],
 * ]);
31
 * NavBar::end();
32 33
 * ```
 *
MarsuBoss committed
34
 * @see http://getbootstrap.com/components/#navbar
35 36 37 38 39
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class NavBar extends Widget
{
40
	/**
Qiang Xue committed
41
	 * @var string the text of the brand. Note that this is not HTML-encoded.
MarsuBoss committed
42
	 * @see http://getbootstrap.com/components/#navbar
43 44 45 46
	 */
	public $brandLabel;
	/**
	 * @param array|string $url the URL for the brand's hyperlink tag. This parameter will be processed by [[Html::url()]]
47
	 * and will be used for the "href" attribute of the brand link. If not set, [[\yii\web\Application::homeUrl]] will be used.
48
	 */
49
	public $brandUrl;
50 51 52
	/**
	 * @var array the HTML attributes of the brand link.
	 */
Alexander Makarov committed
53
	public $brandOptions = [];
54 55 56
	/**
	 * @var string text to show for screen readers for the button to toggle the navbar.
	 */
57
	public $screenReaderToggleText = 'Toggle navigation';
58 59 60 61 62
	/**
	 * @var bool whether the navbar content should be included in a `container` div which adds left and right padding.
	 * Set this to false for a 100% width navbar.
	 */
	public $padded = true;
63 64 65 66 67 68 69

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
70
		$this->clientOptions = false;
71 72 73 74
		Html::addCssClass($this->options, 'navbar');
		if ($this->options['class'] == 'navbar') {
			Html::addCssClass($this->options, 'navbar-default');
		}
75
		Html::addCssClass($this->brandOptions, 'navbar-brand');
76 77 78
		if (empty($this->options['role'])) {
			$this->options['role'] = 'navigation';
		}
79

80
		echo Html::beginTag('nav', $this->options);
81 82 83
		if ($this->padded) {
			echo Html::beginTag('div', ['class' => 'container']);
		}
84

Alexander Makarov committed
85
		echo Html::beginTag('div', ['class' => 'navbar-header']);
86
		echo $this->renderToggleButton();
87
		if ($this->brandLabel !== null) {
88
			echo Html::a($this->brandLabel, $this->brandUrl === null ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
89
		}
90 91
		echo Html::endTag('div');

Alex-Code committed
92
		echo Html::beginTag('div', ['class' => "collapse navbar-collapse navbar-{$this->options['id']}-collapse"]);
93 94 95
	}

	/**
96
	 * Renders the widget.
97
	 */
98
	public function run()
99
	{
100 101

		echo Html::endTag('div');
102 103 104
		if ($this->padded) {
			echo Html::endTag('div');
		}
105
		echo Html::endTag('nav');
106
		BootstrapPluginAsset::register($this->getView());
107 108 109 110 111 112 113 114
	}

	/**
	 * Renders collapsible toggle button.
	 * @return string the rendering toggle button.
	 */
	protected function renderToggleButton()
	{
Alexander Makarov committed
115
		$bar = Html::tag('span', '', ['class' => 'icon-bar']);
116
		$screenReader = '<span class="sr-only">'.$this->screenReaderToggleText.'</span>';
Alexander Makarov committed
117
		return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", [
118
			'class' => 'navbar-toggle',
119
			'data-toggle' => 'collapse',
Alex-Code committed
120
			'data-target' => ".navbar-{$this->options['id']}-collapse",
Alexander Makarov committed
121
		]);
122
	}
Qiang Xue committed
123
}