mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
refactored NavBar and basic app navbar.
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
use yii\bootstrap\Nav;
|
||||||
|
use yii\bootstrap\NavBar;
|
||||||
use yii\helpers\Html;
|
use yii\helpers\Html;
|
||||||
use yii\widgets\Menu;
|
use yii\widgets\Menu;
|
||||||
use yii\widgets\Breadcrumbs;
|
use yii\widgets\Breadcrumbs;
|
||||||
@ -19,24 +21,26 @@ app\config\AppAsset::register($this);
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<?php $this->beginBody(); ?>
|
<?php $this->beginBody(); ?>
|
||||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
<?php
|
||||||
<div class="container">
|
NavBar::begin(array(
|
||||||
<?php echo Html::a('My Company', Yii::$app->homeUrl, array('class' => 'navbar-brand')); ?>
|
'brandLabel' => 'My Company',
|
||||||
<div class="nav-collapse collapse pull-right">
|
'brandUrl' => Yii::$app->homeUrl,
|
||||||
<?php echo Menu::widget(array(
|
'options' => array(
|
||||||
'options' => array('class' => 'nav navbar-nav'),
|
'class' => 'navbar-inverse navbar-fixed-top',
|
||||||
'items' => array(
|
),
|
||||||
array('label' => 'Home', 'url' => array('/site/index')),
|
));
|
||||||
array('label' => 'About', 'url' => array('/site/about')),
|
echo Menu::widget(array(
|
||||||
array('label' => 'Contact', 'url' => array('/site/contact')),
|
'options' => array('class' => 'nav navbar-nav pull-right'),
|
||||||
Yii::$app->user->isGuest ?
|
'items' => array(
|
||||||
array('label' => 'Login', 'url' => array('/site/login')) :
|
array('label' => 'Home', 'url' => array('/site/index')),
|
||||||
array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')),
|
array('label' => 'About', 'url' => array('/site/about')),
|
||||||
),
|
array('label' => 'Contact', 'url' => array('/site/contact')),
|
||||||
)); ?>
|
Yii::$app->user->isGuest ?
|
||||||
</div>
|
array('label' => 'Login', 'url' => array('/site/login')) :
|
||||||
</div>
|
array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')),
|
||||||
</div>
|
)));
|
||||||
|
NavBar::end();
|
||||||
|
?>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<?php echo Breadcrumbs::widget(array(
|
<?php echo Breadcrumbs::widget(array(
|
||||||
|
@ -7,55 +7,27 @@
|
|||||||
|
|
||||||
namespace yii\bootstrap;
|
namespace yii\bootstrap;
|
||||||
|
|
||||||
use yii\base\InvalidConfigException;
|
|
||||||
use yii\helpers\ArrayHelper;
|
|
||||||
use yii\helpers\Html;
|
use yii\helpers\Html;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NavBar renders a navbar HTML component.
|
* NavBar renders a navbar HTML component.
|
||||||
*
|
*
|
||||||
* For example:
|
* 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,
|
||||||
*
|
*
|
||||||
* ```php
|
* ```php
|
||||||
* echo NavBar::widget(array(
|
* use yii\bootstrap\NavBar;
|
||||||
* 'brandLabel' => 'NavBar Test',
|
* use yii\widgets\Menu;
|
||||||
|
*
|
||||||
|
* NavBar::begin(array('brandLabel' => 'NavBar Test'));
|
||||||
|
* echo Menu::widget(array(
|
||||||
* 'items' => array(
|
* 'items' => array(
|
||||||
* // a Nav widget
|
* array('label' => 'Home', 'url' => array('/site/index')),
|
||||||
* array(
|
* array('label' => 'About', 'url' => array('/site/about')),
|
||||||
* // defaults to Nav anyway.
|
|
||||||
* 'class' => 'yii\bootstrap\Nav',
|
|
||||||
* // widget configuration
|
|
||||||
* 'options' => array(
|
|
||||||
* 'items' => array(
|
|
||||||
* array(
|
|
||||||
* 'label' => 'Home',
|
|
||||||
* 'url' => '/',
|
|
||||||
* 'options' => array('class' => 'active'),
|
|
||||||
* ),
|
|
||||||
* array(
|
|
||||||
* 'label' => 'Dropdown',
|
|
||||||
* 'content' => new Dropdown(array(
|
|
||||||
* 'items' => array(
|
|
||||||
* array(
|
|
||||||
* 'label' => 'DropdownA',
|
|
||||||
* 'url' => '#',
|
|
||||||
* ),
|
|
||||||
* array(
|
|
||||||
* 'label' => 'DropdownB',
|
|
||||||
* 'url' => '#'
|
|
||||||
* ),
|
|
||||||
* )
|
|
||||||
* ),
|
|
||||||
* ),
|
|
||||||
* )
|
|
||||||
* ),
|
|
||||||
* ),
|
|
||||||
* // you can also use strings
|
|
||||||
* '<form class="navbar-search pull-left" action="">' .
|
|
||||||
* '<input type="text" class="search-query" placeholder="Search">' .
|
|
||||||
* '</form>',
|
|
||||||
* ),
|
* ),
|
||||||
* ));
|
* ));
|
||||||
|
* NavBar::end();
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @see http://twitter.github.io/bootstrap/components.html#navbar
|
* @see http://twitter.github.io/bootstrap/components.html#navbar
|
||||||
@ -64,6 +36,10 @@ use yii\helpers\Html;
|
|||||||
*/
|
*/
|
||||||
class NavBar extends Widget
|
class NavBar extends Widget
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var boolean whether to enable a collapsing responsive navbar.
|
||||||
|
*/
|
||||||
|
public $responsive = true;
|
||||||
/**
|
/**
|
||||||
* @var string the text of the brand.
|
* @var string the text of the brand.
|
||||||
* @see http://twitter.github.io/bootstrap/components.html#navbar
|
* @see http://twitter.github.io/bootstrap/components.html#navbar
|
||||||
@ -78,26 +54,6 @@ class NavBar extends Widget
|
|||||||
* @var array the HTML attributes of the brand link.
|
* @var array the HTML attributes of the brand link.
|
||||||
*/
|
*/
|
||||||
public $brandOptions = array();
|
public $brandOptions = array();
|
||||||
/**
|
|
||||||
* @var array list of menu items in the navbar widget. Each array element represents a single
|
|
||||||
* menu item with the following structure:
|
|
||||||
*
|
|
||||||
* ```php
|
|
||||||
* array(
|
|
||||||
* // optional, the menu item class type of the widget to render. Defaults to "Nav" widget.
|
|
||||||
* 'class' => 'Menu item class type',
|
|
||||||
* // required, the configuration options of the widget.
|
|
||||||
* 'options' => array(...),
|
|
||||||
* ),
|
|
||||||
* // optionally, you can pass a string
|
|
||||||
* '<form class="navbar-search pull-left" action="">' .
|
|
||||||
* '<input type="text" class="search-query span2" placeholder="Search">' .
|
|
||||||
* '</form>',
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* Optionally, you can also use a plain string instead of an array element.
|
|
||||||
*/
|
|
||||||
public $items = array();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,7 +64,17 @@ class NavBar extends Widget
|
|||||||
parent::init();
|
parent::init();
|
||||||
$this->clientOptions = false;
|
$this->clientOptions = false;
|
||||||
Html::addCssClass($this->options, 'navbar');
|
Html::addCssClass($this->options, 'navbar');
|
||||||
Html::addCssClass($this->brandOptions, 'brand');
|
Html::addCssClass($this->brandOptions, 'navbar-brand');
|
||||||
|
|
||||||
|
echo Html::beginTag('div', $this->options);
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,68 +82,25 @@ class NavBar extends Widget
|
|||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
echo Html::beginTag('div', $this->options);
|
if ($this->responsive) {
|
||||||
echo $this->renderItems();
|
echo Html::endTag('div');
|
||||||
|
echo Html::endTag('div');
|
||||||
|
}
|
||||||
echo Html::endTag('div');
|
echo Html::endTag('div');
|
||||||
BootstrapPluginAsset::register($this->getView());
|
BootstrapPluginAsset::register($this->getView());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders the items.
|
|
||||||
* @return string the rendering items.
|
|
||||||
*/
|
|
||||||
protected function renderItems()
|
|
||||||
{
|
|
||||||
$items = array();
|
|
||||||
foreach ($this->items as $item) {
|
|
||||||
$items[] = $this->renderItem($item);
|
|
||||||
}
|
|
||||||
$contents = implode("\n", $items);
|
|
||||||
$brand = Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);
|
|
||||||
|
|
||||||
$contents = Html::tag('div',
|
|
||||||
$this->renderToggleButton() .
|
|
||||||
$brand . "\n" .
|
|
||||||
Html::tag('div', $contents, array('class' => 'nav-collapse collapse navbar-collapse')),
|
|
||||||
array('class' => 'container'));
|
|
||||||
|
|
||||||
return Html::tag('div', $contents, array('class' => 'navbar-inner'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders a item. The item can be a string, a custom class or a Nav widget (defaults if no class specified.
|
|
||||||
* @param mixed $item the item to render. If array, it is assumed the configuration of a widget being `class`
|
|
||||||
* required and if not specified, then defaults to `yii\bootstrap\Nav`.
|
|
||||||
* @return string the rendering result.
|
|
||||||
* @throws InvalidConfigException
|
|
||||||
*/
|
|
||||||
protected function renderItem($item)
|
|
||||||
{
|
|
||||||
if (is_string($item)) {
|
|
||||||
return $item;
|
|
||||||
}
|
|
||||||
$config = ArrayHelper::getValue($item, 'options', array());
|
|
||||||
$config['clientOptions'] = false;
|
|
||||||
|
|
||||||
$class = ArrayHelper::getValue($item, 'class', 'yii\bootstrap\Nav');
|
|
||||||
|
|
||||||
return $class::widget($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders collapsible toggle button.
|
* Renders collapsible toggle button.
|
||||||
* @return string the rendering toggle button.
|
* @return string the rendering toggle button.
|
||||||
*/
|
*/
|
||||||
protected function renderToggleButton()
|
protected function renderToggleButton()
|
||||||
{
|
{
|
||||||
$items = array();
|
$bar = Html::tag('span', '', array('class' => 'icon-bar'));
|
||||||
for ($i = 0; $i < 3; $i++) {
|
return Html::button("{$bar}\n{$bar}\n{$bar}", array(
|
||||||
$items[] = Html::tag('span', '', array('class' => 'icon-bar'));
|
'class' => 'navbar-toggle',
|
||||||
}
|
|
||||||
return Html::a(implode("\n", $items), null, array(
|
|
||||||
'class' => 'btn btn-navbar',
|
|
||||||
'data-toggle' => 'collapse',
|
'data-toggle' => 'collapse',
|
||||||
'data-target' => 'div.navbar-collapse',
|
'data-target' => '.navbar-responsive-collapse',
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user