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

namespace yii\bootstrap;

use yii\helpers\Html;

/**
 * NavBar renders a navbar HTML component.
 *
15 16 17
 * 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,
18 19
 *
 * ```php
20 21 22 23
 * use yii\bootstrap\NavBar;
 * use yii\widgets\Menu;
 *
 * NavBar::begin(array('brandLabel' => 'NavBar Test'));
Qiang Xue committed
24
 * echo Nav::widget(array(
25
 *     'items' => array(
26 27
 *         array('label' => 'Home', 'url' => array('/site/index')),
 *         array('label' => 'About', 'url' => array('/site/about')),
28 29
 *     ),
 * ));
30
 * NavBar::end();
31 32 33 34 35 36 37 38
 * ```
 *
 * @see http://twitter.github.io/bootstrap/components.html#navbar
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class NavBar extends Widget
{
39 40 41 42
	/**
	 * @var boolean whether to enable a collapsing responsive navbar.
	 */
	public $responsive = true;
43
	/**
Qiang Xue committed
44
	 * @var string the text of the brand. Note that this is not HTML-encoded.
45 46 47 48 49 50 51
	 * @see http://twitter.github.io/bootstrap/components.html#navbar
	 */
	public $brandLabel;
	/**
	 * @param array|string $url the URL for the brand's hyperlink tag. This parameter will be processed by [[Html::url()]]
	 * and will be used for the "href" attribute of the brand link. Defaults to site root.
	 */
Qiang Xue committed
52
	public $brandUrl = '/';
53 54 55 56
	/**
	 * @var array the HTML attributes of the brand link.
	 */
	public $brandOptions = array();
57 58 59 60 61 62 63 64


	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
65
		$this->clientOptions = false;
66
		Html::addCssClass($this->options, 'navbar');
67
		Html::addCssClass($this->brandOptions, 'navbar-brand');
68 69

		echo Html::beginTag('div', $this->options);
70 71 72 73 74 75 76
		if ($this->responsive) {
			echo Html::beginTag('div', array('class' => 'container'));
			echo $this->renderToggleButton();
			echo Html::beginTag('div', array('class' => 'nav-collapse collapse navbar-responsive-collapse'));
		}
		if ($this->brandLabel !== null) {
			echo Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);
77 78 79 80
		}
	}

	/**
81
	 * Renders the widget.
82
	 */
83
	public function run()
84
	{
85 86 87
		if ($this->responsive) {
			echo Html::endTag('div');
			echo Html::endTag('div');
88
		}
89 90
		echo Html::endTag('div');
		BootstrapPluginAsset::register($this->getView());
91 92 93 94 95 96 97 98
	}

	/**
	 * Renders collapsible toggle button.
	 * @return string the rendering toggle button.
	 */
	protected function renderToggleButton()
	{
99 100 101
		$bar = Html::tag('span', '', array('class' => 'icon-bar'));
		return Html::button("{$bar}\n{$bar}\n{$bar}", array(
			'class' => 'navbar-toggle',
102
			'data-toggle' => 'collapse',
103
			'data-target' => '.navbar-responsive-collapse',
104
		));
105
	}
Qiang Xue committed
106
}