Final Class Yiisoft\Yii\Widgets\Menu
| Inheritance | Yiisoft\Yii\Widgets\Menu » Yiisoft\Widget\Widget |
|---|
Menu displays a multi-level menu using nested HTML lists.
The {@see \Yiisoft\Yii\Widgets\Menu::items()} method specifies the possible items in the menu. 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 with active state.
Note that Menu only renders the HTML tags about the menu. It does not do any styling. You are responsible to provide CSS styles to make it look like a real menu.
The following example shows how to use Menu:
<?= Menu::Widget()
->items([
['label' => 'Login', 'link' => 'site/login', 'visible' => true],
]);
?>
Public Methods
| Method | Description | Defined By |
|---|---|---|
| activateItems() | Return new instance with specified whether to activate parent menu items when one of the corresponding child menu items is active. | Yiisoft\Yii\Widgets\Menu |
| activeClass() | Returns a new instance with the specified active CSS class. | Yiisoft\Yii\Widgets\Menu |
| afterAttributes() | Returns a new instance with the specified after container attributes. | Yiisoft\Yii\Widgets\Menu |
| afterClass() | Returns a new instance with the specified after container class. | Yiisoft\Yii\Widgets\Menu |
| afterContent() | Returns a new instance with the specified after content. | Yiisoft\Yii\Widgets\Menu |
| afterTag() | Returns a new instance with the specified after container tag. | Yiisoft\Yii\Widgets\Menu |
| attributes() | Returns a new instance with the HTML attributes. The following special options are recognized. | Yiisoft\Yii\Widgets\Menu |
| beforeAttributes() | Returns a new instance with the specified before container attributes. | Yiisoft\Yii\Widgets\Menu |
| beforeClass() | Returns a new instance with the specified before container class. | Yiisoft\Yii\Widgets\Menu |
| beforeContent() | Returns a new instance with the specified before content. | Yiisoft\Yii\Widgets\Menu |
| beforeTag() | Returns a new instance with the specified before container tag. | Yiisoft\Yii\Widgets\Menu |
| class() | Returns a new instance with the specified the class menu widget. |
Yiisoft\Yii\Widgets\Menu |
| container() | Returns a new instance with the specified enable or disable the container widget. | Yiisoft\Yii\Widgets\Menu |
| currentPath() | Returns a new instance with the specified the current path. | Yiisoft\Yii\Widgets\Menu |
| disabledClass() | Returns a new instance with the specified disabled CSS class. | Yiisoft\Yii\Widgets\Menu |
| dropdownContainerClass() | Returns a new instance with the specified dropdown container class. | Yiisoft\Yii\Widgets\Menu |
| dropdownContainerTag() | Returns a new instance with the specified dropdown container tag. | Yiisoft\Yii\Widgets\Menu |
| dropdownDefinitions() | Returns a new instance with the specified dropdown definition widget. | Yiisoft\Yii\Widgets\Menu |
| firstItemClass() | Returns a new instance with the specified first item CSS class. | Yiisoft\Yii\Widgets\Menu |
| iconContainerAttributes() | Returns a new instance with the specified icon container attributes. | Yiisoft\Yii\Widgets\Menu |
| items() | List of items in the nav widget. Each array element represents a single menu item which can be either a string or an array with the following structure: | Yiisoft\Yii\Widgets\Menu |
| itemsContainer() | Returns a new instance with the specified if enabled or disabled the items' container. | Yiisoft\Yii\Widgets\Menu |
| itemsContainerAttributes() | Returns a new instance with the specified items' container attributes. | Yiisoft\Yii\Widgets\Menu |
| itemsContainerClass() | Returns a new instance with the specified items' container class. | Yiisoft\Yii\Widgets\Menu |
| itemsTag() | Returns a new instance with the specified items tag. | Yiisoft\Yii\Widgets\Menu |
| lastItemClass() | Returns a new instance with the specified last item CSS class. | Yiisoft\Yii\Widgets\Menu |
| linkAttributes() | Returns a new instance with the specified link attributes. | Yiisoft\Yii\Widgets\Menu |
| linkClass() | Returns a new instance with the specified link css class. | Yiisoft\Yii\Widgets\Menu |
| linkTag() | Returns a new instance with the specified link tag. | Yiisoft\Yii\Widgets\Menu |
| render() | Renders the menu. | Yiisoft\Yii\Widgets\Menu |
| tagName() | Returns a new instance with the specified tag for rendering the menu. | Yiisoft\Yii\Widgets\Menu |
| template() | Returns a new instance with the specified the template used to render the main menu. | Yiisoft\Yii\Widgets\Menu |
Method Details
Return new instance with specified whether to activate parent menu items when one of the corresponding child menu items is active.
| public self activateItems ( boolean $value ) | ||
| $value | boolean |
The value to be assigned to the activateItems property. |
public function activateItems(bool $value): self
{
$new = clone $this;
$new->activateItems = $value;
return $new;
}
Returns a new instance with the specified active CSS class.
| public self activeClass ( string $value ) | ||
| $value | string |
The CSS class to be appended to the active menu item. |
public function activeClass(string $value): self
{
$new = clone $this;
$new->activeClass = $value;
return $new;
}
Returns a new instance with the specified after container attributes.
| public self afterAttributes ( array $valuesMap ) | ||
| $valuesMap | array |
Attribute values indexed by attribute names. |
public function afterAttributes(array $valuesMap): self
{
$new = clone $this;
$new->afterAttributes = $valuesMap;
return $new;
}
Returns a new instance with the specified after container class.
| public self afterClass ( string $value ) | ||
| $value | string |
The class name. |
public function afterClass(string $value): self
{
$new = clone $this;
Html::addCssClass($new->afterAttributes, $value);
return $new;
}
Returns a new instance with the specified after content.
| public self afterContent ( string|\Stringable $content ) | ||
| $content | string|\Stringable |
The content. |
public function afterContent(string|Stringable $content): self
{
$new = clone $this;
$new->afterContent = (string) $content;
return $new;
}
Returns a new instance with the specified after container tag.
| public self afterTag ( string $value ) | ||
| $value | string |
The after container tag. |
public function afterTag(string $value): self
{
$new = clone $this;
$new->afterTag = $value;
return $new;
}
Returns a new instance with the HTML attributes. The following special options are recognized.
| public self attributes ( array $valuesMap ) | ||
| $valuesMap | array |
Attribute values indexed by attribute names. |
public function attributes(array $valuesMap): self
{
$new = clone $this;
$new->attributes = $valuesMap;
return $new;
}
Returns a new instance with the specified before container attributes.
| public self beforeAttributes ( array $valuesMap ) | ||
| $valuesMap | array |
Attribute values indexed by attribute names. |
public function beforeAttributes(array $valuesMap): self
{
$new = clone $this;
$new->beforeAttributes = $valuesMap;
return $new;
}
Returns a new instance with the specified before container class.
| public self beforeClass ( string $value ) | ||
| $value | string |
The before container class. |
public function beforeClass(string $value): self
{
$new = clone $this;
Html::addCssClass($new->beforeAttributes, $value);
return $new;
}
Returns a new instance with the specified before content.
| public self beforeContent ( string|\Stringable $value ) | ||
| $value | string|\Stringable |
The content. |
public function beforeContent(string|Stringable $value): self
{
$new = clone $this;
$new->beforeContent = (string) $value;
return $new;
}
Returns a new instance with the specified before container tag.
| public self beforeTag ( string $value ) | ||
| $value | string |
The before container tag. |
public function beforeTag(string $value): self
{
$new = clone $this;
$new->beforeTag = $value;
return $new;
}
Returns a new instance with the specified the class menu widget.
| public self class ( string $value ) | ||
| $value | string |
The class |
public function class(string $value): self
{
$new = clone $this;
Html::addCssClass($new->attributes, $value);
return $new;
}
Returns a new instance with the specified enable or disable the container widget.
| public self container ( boolean $value ) | ||
| $value | boolean |
The container widget enable or disable, for default is |
public function container(bool $value): self
{
$new = clone $this;
$new->container = $value;
return $new;
}
Returns a new instance with the specified the current path.
| public self currentPath ( string $value ) | ||
| $value | string |
The current path. |
public function currentPath(string $value): self
{
$new = clone $this;
$new->currentPath = $value;
return $new;
}
Returns a new instance with the specified disabled CSS class.
| public self disabledClass ( string $value ) | ||
| $value | string |
The CSS class to be appended to the disabled menu item. |
public function disabledClass(string $value): self
{
$new = clone $this;
$new->disabledClass = $value;
return $new;
}
Returns a new instance with the specified dropdown container class.
| public self dropdownContainerClass ( string $value ) | ||
| $value | string |
The dropdown container class. |
public function dropdownContainerClass(string $value): self
{
$new = clone $this;
Html::addCssClass($new->dropdownContainerAttributes, $value);
return $new;
}
Returns a new instance with the specified dropdown container tag.
| public self dropdownContainerTag ( string $value ) | ||
| $value | string |
The dropdown container tag. |
public function dropdownContainerTag(string $value): self
{
$new = clone $this;
$new->dropdownContainerTag = $value;
return $new;
}
Returns a new instance with the specified dropdown definition widget.
| public self dropdownDefinitions ( array $valuesMap ) | ||
| $valuesMap | array |
The dropdown definition widget. |
public function dropdownDefinitions(array $valuesMap): self
{
$new = clone $this;
$new->dropdownDefinitions = $valuesMap;
return $new;
}
Returns a new instance with the specified first item CSS class.
| public self firstItemClass ( string $value ) | ||
| $value | string |
The CSS class that will be assigned to the first item in the main menu or each submenu. |
public function firstItemClass(string $value): self
{
$new = clone $this;
$new->firstItemClass = $value;
return $new;
}
Returns a new instance with the specified icon container attributes.
| public self iconContainerAttributes ( array $valuesMap ) | ||
| $valuesMap | array |
Attribute values indexed by attribute names. |
public function iconContainerAttributes(array $valuesMap): self
{
$new = clone $this;
$new->iconContainerAttributes = $valuesMap;
return $new;
}
List of items in the nav widget. Each array element represents a single menu item which can be either a string or an array with the following structure:
- label: string, required, the nav item label.
- active: bool, whether the item should be on active state or not.
- disabled: bool, whether the item should be on disabled state or not. For default
disabledis false. - encode: bool, whether the label should be HTML encoded or not. For default
encodeLabelis true. - items: array, optional, the item's submenu items. The structure is the same as for
itemsoption. - itemsContainerAttributes: array, optional, the HTML attributes for the item's submenu container.
- link: string, the item's href. Defaults to "#". For default
linkis "#". - linkAttributes: array, the HTML attributes of the item's link. For default
linkAttributesis[]. - icon: string, the item's icon. For default is ``.
- iconAttributes: array, the HTML attributes of the item's icon. For default
iconAttributesis[]. - iconClass: string, the item's icon CSS class. For default is ``.
- visible: bool, optional, whether this menu item is visible. Defaults to true.
If a menu item is a string, it will be rendered directly without HTML encoding.
| public self items ( array $valuesMap ) | ||
| $valuesMap | array |
The list of items to be rendered. |
public function items(array $valuesMap): self
{
$new = clone $this;
$new->items = $valuesMap;
return $new;
}
Returns a new instance with the specified if enabled or disabled the items' container.
| public self itemsContainer ( boolean $value ) | ||
| $value | boolean |
The items container enable or disable, for default is |
public function itemsContainer(bool $value): self
{
$new = clone $this;
$new->itemsContainer = $value;
return $new;
}
Returns a new instance with the specified items' container attributes.
| public self itemsContainerAttributes ( array $valuesMap ) | ||
| $valuesMap | array |
Attribute values indexed by attribute names. |
public function itemsContainerAttributes(array $valuesMap): self
{
$new = clone $this;
$new-> itemsContainerAttributes = $valuesMap;
return $new;
}
Returns a new instance with the specified items' container class.
| public self itemsContainerClass ( string $value ) | ||
| $value | string |
The CSS class that will be assigned to the items' container. |
public function itemsContainerClass(string $value): self
{
$new = clone $this;
Html::addCssClass($new->itemsContainerAttributes, $value);
return $new;
}
Returns a new instance with the specified items tag.
| public self itemsTag ( string $value ) | ||
| $value | string |
The tag that will be used to wrap the items. |
public function itemsTag(string $value): self
{
$new = clone $this;
$new->itemsTag = $value;
return $new;
}
Returns a new instance with the specified last item CSS class.
| public self lastItemClass ( string $value ) | ||
| $value | string |
The CSS class that will be assigned to the last item in the main menu or each submenu. |
public function lastItemClass(string $value): self
{
$new = clone $this;
$new->lastItemClass = $value;
return $new;
}
Returns a new instance with the specified link attributes.
| public self linkAttributes ( array $valuesMap ) | ||
| $valuesMap | array |
Attribute values indexed by attribute names. |
public function linkAttributes(array $valuesMap): self
{
$new = clone $this;
$new->linkAttributes = $valuesMap;
return $new;
}
Returns a new instance with the specified link css class.
| public self linkClass ( string $value ) | ||
| $value | string |
The CSS class that will be assigned to the link. |
public function linkClass(string $value): self
{
$new = clone $this;
$new->linkClass = $value;
return $new;
}
Returns a new instance with the specified link tag.
| public self linkTag ( string $value ) | ||
| $value | string |
The tag that will be used to wrap the link. |
public function linkTag(string $value): self
{
$new = clone $this;
$new->linkTag = $value;
return $new;
}
Renders the menu.
| public string render ( ) | ||
| return | string |
The result of Widget execution to be outputted. |
|---|---|---|
| throws | \Yiisoft\Definitions\Exception\CircularReferenceException|\Yiisoft\Definitions\Exception\InvalidConfigException|\Yiisoft\Factory\NotFoundException|\Yiisoft\Definitions\Exception\NotInstantiableException | |
public function render(): string
{
if ($this->items === []) {
return '';
}
/**
* @psalm-var array<
* array-key,
* array{
* label: string,
* link: string,
* linkAttributes: array,
* active: bool,
* disabled: bool,
* visible: bool,
* items?: array
* }
* > $items
*/
$items = Helper\Normalizer::menu(
$this->items,
$this->currentPath,
$this->activateItems,
$this->iconContainerAttributes,
);
return $this->renderMenu($items);
}
Returns a new instance with the specified tag for rendering the menu.
| public self tagName ( string $value ) | ||
| $value | string |
The tag for rendering the menu. |
public function tagName(string $value): self
{
$new = clone $this;
$new->tagName = $value;
return $new;
}
Returns a new instance with the specified the template used to render the main menu.
| public self template ( string $value ) | ||
| $value | string |
The template used to render the main menu. In this template, the token |
public function template(string $value): self
{
$new = clone $this;
$new->template = $value;
return $new;
}
Signup or Login in order to comment.