CMenu
Package | zii.widgets |
---|---|
Inheritance | class CMenu » CWidget » CBaseController » CComponent |
Since | 1.1 |
Version | $Id: CMenu.php 185 2010-06-16 20:56:20Z qiang.xue $ |
The main property of CMenu is items, which specifies the possible items in the menu. A menu item has three main properties: visible, active and items. The "visible" property specifies whether the menu item is currently visible. The "active" property specifies whether the menu item is currently selected. And the "items" property specifies the child menu items.
The following example shows how to use CMenu:
$this->widget('zii.widgets.CMenu', array( 'items'=>array( array('label'=>'Home', 'url'=>array('site/index')), array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array( array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')), array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')), )), array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest), ), ));
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
actionPrefix | string | the prefix to the IDs of the actions. | CWidget |
activateItems | boolean | whether to automatically activate items according to whether their route setting matches the currently requested route. | CMenu |
activateParents | boolean | whether to activate parent menu items when one of the corresponding child menu items is active. | CMenu |
activeCssClass | string | the CSS class to be appended to the active menu item. | CMenu |
controller | CController | the controller that this widget belongs to. | CWidget |
encodeLabel | boolean | whether the labels for menu items should be HTML-encoded. | CMenu |
hideEmptyItems | boolean | whether to hide empty menu items. | CMenu |
htmlOptions | array | HTML attributes for the menu's root container tag | CMenu |
id | string | id of the widget. | CWidget |
itemTemplate | string | the template used to render an individual menu item. | CMenu |
items | array | list of menu items. | CMenu |
owner | CBaseController | owner/creator of this widget. | CWidget |
skin | mixed | the name of the skin to be used by this widget. | CWidget |
submenuHtmlOptions | array | HTML attributes for the submenu's container tag. | CMenu |
viewPath | string | Returns the directory containing the view files for this widget. | CWidget |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CWidget |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
actions() | Returns a list of actions that are used by this widget. | CWidget |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
beginCache() | Begins fragment caching. | CBaseController |
beginClip() | Begins recording a clip. | CBaseController |
beginContent() | Begins the rendering of content that is to be decorated by the specified view. | CBaseController |
beginWidget() | Creates a widget and executes it. | CBaseController |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
createWidget() | Creates a widget and initializes it. | CBaseController |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
endCache() | Ends fragment caching. | CBaseController |
endClip() | Ends recording a clip. | CBaseController |
endContent() | Ends the rendering of content. | CBaseController |
endWidget() | Ends the execution of the named widget. | CBaseController |
evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
getController() | CWidget | |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getId() | CWidget | |
getOwner() | CWidget | |
getViewFile() | Looks for the view script file according to the view name. | CWidget |
getViewPath() | Returns the directory containing the view files for this widget. | CWidget |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
init() | Initializes the menu widget. | CMenu |
raiseEvent() | Raises an event. | CComponent |
render() | Renders a view. | CWidget |
renderFile() | Renders a view file. | CBaseController |
renderInternal() | Renders a view file. | CBaseController |
run() | Calls renderMenu to render the menu. | CMenu |
setId() | CWidget | |
widget() | Creates a widget and executes it. | CBaseController |
Protected Methods
Method | Description | Defined By |
---|---|---|
isItemActive() | Checks whether a menu item is active. | CMenu |
normalizeItems() | Normalizes the items property so that the 'active' state is properly identified for every menu item. | CMenu |
renderMenu() | Renders the menu items. | CMenu |
renderMenuRecursive() | Recursively renders the menu items. | CMenu |
Property Details
whether to automatically activate items according to whether their route setting matches the currently requested route. Defaults to true.
whether to activate parent menu items when one of the corresponding child menu items is active. The activated parent menu items will also have its CSS classes appended with activeCssClass. Defaults to false.
the CSS class to be appended to the active menu item. Defaults to 'active'. If empty, the CSS class of menu items will not be changed.
whether the labels for menu items should be HTML-encoded. Defaults to true.
whether to hide empty menu items. An empty menu item is one whose 'url' option is not set and which doesn't contain visible child menu items. Defaults to true.
HTML attributes for the menu's root container tag
the template used to render an individual menu item. In this template, the token "{menu}" will be replaced with the corresponding menu link or text. If this property is not set, each menu will be rendered without any decoration. This property will be overridden by the 'template' option set in individual menu items via {@items}.
list of menu items. Each menu item is specified as an array of name-value pairs. Possible option names include the following:
- label: string, required, specifies the menu item label. When encodeLabel is true, the label will be HTML-encoded.
- url: string or array, optional, specifies the URL of the menu item. It is passed to CHtml::normalizeUrl to generate a valid URL. If this is not set, the menu item will be rendered as a span text.
- visible: boolean, optional, whether this menu item is visible. Defaults to true. This can be used to control the visibility of menu items based on user permissions.
- items: array, optional, specifies the sub-menu items. Its format is the same as the parent items.
- active: boolean, optional, whether this menu item is in active state (currently selected). If a menu item is active and activeClass is not empty, its CSS class will be appended with activeClass. If this option is not set, the menu item will be set active automatically when the current request is triggered by url. Note that the GET parameters not specified in the 'url' option will be ignored.
- template: string, optional, the template used to render this menu item. In this template, the token "{menu}" will be replaced with the corresponding menu link or text. Please see itemTemplate for more details. This option has been available since version 1.1.1.
- linkOptions: array, optional, additional HTML attributes to be rendered for the link or span tag of the menu item.
- itemOptions: array, optional, additional HTML attributes to be rendered for the container tag of the menu item.
HTML attributes for the submenu's container tag.
Method Details
public void init()
|
Initializes the menu widget. This method mainly normalizes the items property. If this method is overridden, make sure the parent implementation is invoked.
protected boolean isItemActive(array $item, string $route)
| ||
$item | array | the menu item to be checked |
$route | string | the route of the current request |
{return} | boolean | whether the menu item is active |
Checks whether a menu item is active. This is done by checking if the currently requested URL is generated by the 'url' option of the menu item. Note that the GET parameters not specified in the 'url' option will be ignored.
protected array normalizeItems(array $items, string $route, boolean $active)
| ||
$items | array | the items to be normalized. |
$route | string | the route of the current request. |
$active | boolean | whether there is an active child menu item. |
{return} | array | the normalized menu items |
Normalizes the items property so that the 'active' state is properly identified for every menu item.
protected void renderMenu(array $items)
| ||
$items | array | menu items. Each menu item will be an array with at least two elements: 'label' and 'active'. It may have three other optional elements: 'items', 'linkOptions' and 'itemOptions'. |
Renders the menu items.
protected void renderMenuRecursive(array $items)
| ||
$items | array | the menu items to be rendered recursively |
Recursively renders the menu items.
public void run()
|
Calls renderMenu to render the menu.