mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-17 07:51:12 +08:00
\yii\widgets\Menu improvement
This commit is contained in:
@ -9,6 +9,7 @@ namespace yii\widgets;
|
|||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\base\Widget;
|
use yii\base\Widget;
|
||||||
|
use yii\helpers\ArrayHelper;
|
||||||
use yii\helpers\Html;
|
use yii\helpers\Html;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,15 +17,15 @@ use yii\helpers\Html;
|
|||||||
*
|
*
|
||||||
* The main property of Menu is [[items]], which specifies the possible items in the menu.
|
* The main property of Menu is [[items]], which specifies the possible items in the menu.
|
||||||
* A menu item can contain sub-items which specify the sub-menu under that menu item.
|
* A menu item can contain sub-items which specify the sub-menu under that menu item.
|
||||||
*
|
*
|
||||||
* Menu checks the current route and request parameters to toggle certain menu items
|
* Menu checks the current route and request parameters to toggle certain menu items
|
||||||
* with active state.
|
* with active state.
|
||||||
*
|
*
|
||||||
* Note that Menu only renders the HTML tags about the menu. It does do any styling.
|
* Note that Menu only renders the HTML tags about the menu. It does do any styling.
|
||||||
* You are responsible to provide CSS styles to make it look like a real menu.
|
* You are responsible to provide CSS styles to make it look like a real menu.
|
||||||
*
|
*
|
||||||
* The following example shows how to use Menu:
|
* The following example shows how to use Menu:
|
||||||
*
|
*
|
||||||
* ~~~
|
* ~~~
|
||||||
* echo Menu::widget(array(
|
* echo Menu::widget(array(
|
||||||
* 'items' => array(
|
* 'items' => array(
|
||||||
@ -40,7 +41,7 @@ use yii\helpers\Html;
|
|||||||
* ),
|
* ),
|
||||||
* ));
|
* ));
|
||||||
* ~~~
|
* ~~~
|
||||||
*
|
*
|
||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
@ -67,6 +68,13 @@ class Menu extends Widget
|
|||||||
* - options: array, optional, the HTML attributes for the menu container tag.
|
* - options: array, optional, the HTML attributes for the menu container tag.
|
||||||
*/
|
*/
|
||||||
public $items = array();
|
public $items = array();
|
||||||
|
/**
|
||||||
|
* @var array list of HTML attributes for the menu container tag. This will be overwritten
|
||||||
|
* by the "options" set in individual [[items]]. The following special options are recognized:
|
||||||
|
*
|
||||||
|
* - tag: string, defaults to "li", the tag name of the item container tags.
|
||||||
|
*/
|
||||||
|
public $itemOptions = array();
|
||||||
/**
|
/**
|
||||||
* @var string the template used to render the body of a menu which is a link.
|
* @var string the template used to render the body of a menu which is a link.
|
||||||
* In this template, the token `{url}` will be replaced with the corresponding link URL;
|
* In this template, the token `{url}` will be replaced with the corresponding link URL;
|
||||||
@ -110,7 +118,9 @@ class Menu extends Widget
|
|||||||
*/
|
*/
|
||||||
public $hideEmptyItems = true;
|
public $hideEmptyItems = true;
|
||||||
/**
|
/**
|
||||||
* @var array the HTML attributes for the menu's container tag.
|
* @var array the HTML attributes for the menu's container tag. The following special options are recognized:
|
||||||
|
*
|
||||||
|
* - tag: string, defaults to "ul", the tag name of the item container tags.
|
||||||
*/
|
*/
|
||||||
public $options = array();
|
public $options = array();
|
||||||
/**
|
/**
|
||||||
@ -125,7 +135,7 @@ class Menu extends Widget
|
|||||||
public $lastItemCssClass;
|
public $lastItemCssClass;
|
||||||
/**
|
/**
|
||||||
* @var string the route used to determine if a menu item is active or not.
|
* @var string the route used to determine if a menu item is active or not.
|
||||||
* If not set, it will use the route of the current request.
|
* If not set, it will use the route of the current request.
|
||||||
* @see params
|
* @see params
|
||||||
* @see isItemActive
|
* @see isItemActive
|
||||||
*/
|
*/
|
||||||
@ -151,7 +161,9 @@ class Menu extends Widget
|
|||||||
$this->params = $_GET;
|
$this->params = $_GET;
|
||||||
}
|
}
|
||||||
$items = $this->normalizeItems($this->items, $hasActiveChild);
|
$items = $this->normalizeItems($this->items, $hasActiveChild);
|
||||||
echo Html::tag('ul', $this->renderItems($items), $this->options);
|
$options = $this->options;
|
||||||
|
$tag = ArrayHelper::remove($options, 'tag', 'ul');
|
||||||
|
echo Html::tag($tag, $this->renderItems($items), $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -164,7 +176,8 @@ class Menu extends Widget
|
|||||||
$n = count($items);
|
$n = count($items);
|
||||||
$lines = array();
|
$lines = array();
|
||||||
foreach ($items as $i => $item) {
|
foreach ($items as $i => $item) {
|
||||||
$options = isset($item['options']) ? $item['options'] : array();
|
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', array()));
|
||||||
|
$tag = ArrayHelper::remove($options, 'tag', 'li');
|
||||||
$class = array();
|
$class = array();
|
||||||
if ($item['active']) {
|
if ($item['active']) {
|
||||||
$class[] = $this->activeCssClass;
|
$class[] = $this->activeCssClass;
|
||||||
@ -189,7 +202,7 @@ class Menu extends Widget
|
|||||||
'{items}' => $this->renderItems($item['items']),
|
'{items}' => $this->renderItems($item['items']),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
$lines[] = Html::tag('li', $menu, $options);
|
$lines[] = Html::tag($tag, $menu, $options);
|
||||||
}
|
}
|
||||||
return implode("\n", $lines);
|
return implode("\n", $lines);
|
||||||
}
|
}
|
||||||
@ -203,13 +216,13 @@ class Menu extends Widget
|
|||||||
protected function renderItem($item)
|
protected function renderItem($item)
|
||||||
{
|
{
|
||||||
if (isset($item['url'])) {
|
if (isset($item['url'])) {
|
||||||
$template = isset($item['template']) ? $item['template'] : $this->linkTemplate;
|
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
|
||||||
return strtr($template, array(
|
return strtr($template, array(
|
||||||
'{url}' => Html::url($item['url']),
|
'{url}' => Html::url($item['url']),
|
||||||
'{label}' => $item['label'],
|
'{label}' => $item['label'],
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
$template = isset($item['template']) ? $item['template'] : $this->labelTemplate;
|
$template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
|
||||||
return strtr($template, array(
|
return strtr($template, array(
|
||||||
'{label}' => $item['label'],
|
'{label}' => $item['label'],
|
||||||
));
|
));
|
||||||
@ -284,5 +297,4 @@ class Menu extends Widget
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user