Yii Framework v1.1.10 绫诲弬鑰

CMenu

zii.widgets
缁ф壙 class CMenu » CWidget » CBaseController » CComponent
婧愯嚜 1.1
鐗堟湰 $Id: CMenu.php 3520 2011-12-29 09:54:22Z mdomba $
婧愮爜 framework/zii/widgets/CMenu.php
CMenu浣跨敤宓屽鐨凥TML鍒楄〃鏄剧ず澶氱骇鑿滃崟銆

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(
        // Important: you need to specify url as 'controller/action',
        // not just as 'controller' even if default acion is used.
        array('label'=>'Home', 'url'=>array('site/index')),
        // 'Products' menu item will be selected no matter which tag parameter value is since it's not specified.
        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),
    ),
));

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
actionPrefix string actions鐨処D鐨勫墠缂銆 褰撳井浠跺湪CController::actions涓0鏄庝簡 鍔ㄤ綔鎻愪緵鑰咃紝鍙互涓哄叾鍔ㄤ綔鐨処D鎸囧畾鍓嶇紑浠ュ尯鍒 浜庡埆鐨勫井浠舵垨鎺у埗鍣ㄣ傚綋寰欢鐢ㄤ簬鎺у埗鍣 鐨勮鍥句腑鏃讹紝蹇呴』閰嶇疆鍚屾牱鐨勫墠缂銆 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 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ CWidget
encodeLabel boolean whether the labels for menu items should be HTML-encoded. CMenu
firstItemCssClass string the CSS class that will be assigned to the first item in the main menu or each submenu. 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銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 CWidget
itemCssClass string the CSS class that will be assigned to every item. CMenu
itemTemplate string the template used to render an individual menu item. CMenu
items array list of menu items. CMenu
lastItemCssClass string the CSS class that will be assigned to the last item in the main menu or each submenu. CMenu
linkLabelWrapper string the HTML element name that will be used to wrap the label of all menu links. CMenu
owner CBaseController 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 CWidget
skin mixed 寰欢浣跨敤鐨勭毊鑲ょ殑鍚嶇О銆傞粯璁や负鈥渄efault鈥濄 濡傛灉姝ゅ睘鎬ц缃负false锛屽井浠跺皢涓嶄細鏈夌毊鑲よ浣跨敤銆 CWidget
viewPath string 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 CWidget

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犲櫒銆 CWidget
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
actions() 杩斿洖姝idget浣跨敤鐨勫姩浣滅殑鍒楄〃銆 CWidget
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 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() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 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() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getController() 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ CWidget
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getId() 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 CWidget
getOwner() 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 CWidget
getViewFile() 鏍规嵁瑙嗗浘鍚嶆煡鎵捐鍥炬枃浠躲 CWidget
getViewPath() 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 CWidget
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() Initializes the menu widget. CMenu
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
render() 娓叉煋涓涓鍥俱 CWidget
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
run() Calls renderMenu to render the menu. CMenu
setId() 璁剧疆姝ゅ井浠剁殑ID銆 CWidget
widget() Creates a widget and executes it. CBaseController

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
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
renderMenuItem() Renders the content of a menu item. CMenu
renderMenuRecursive() Recursively renders the menu items. CMenu

灞炴ц缁

activateItems 灞炴 锛堝彲鐢ㄨ嚜 v1.1.3锛
public boolean $activateItems;

whether to automatically activate items according to whether their route setting matches the currently requested route. Defaults to true.

activateParents 灞炴
public boolean $activateParents;

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.

activeCssClass 灞炴
public string $activeCssClass;

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.

encodeLabel 灞炴
public boolean $encodeLabel;

whether the labels for menu items should be HTML-encoded. Defaults to true.

firstItemCssClass 灞炴 锛堝彲鐢ㄨ嚜 v1.1.4锛
public string $firstItemCssClass;

the CSS class that will be assigned to the first item in the main menu or each submenu. Defaults to null, meaning no such CSS class will be assigned.

hideEmptyItems 灞炴
public boolean $hideEmptyItems;

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.

htmlOptions 灞炴
public array $htmlOptions;

HTML attributes for the menu's root container tag

itemCssClass 灞炴 锛堝彲鐢ㄨ嚜 v1.1.9锛
public string $itemCssClass;

the CSS class that will be assigned to every item. Defaults to null, meaning no such CSS class will be assigned.

itemTemplate 灞炴 锛堝彲鐢ㄨ嚜 v1.1.1锛
public string $itemTemplate;

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}.

items 灞炴
public array $items;

list of menu items. Each menu item is specified as an array of name-value pairs. Possible option names include the following:

lastItemCssClass 灞炴 锛堝彲鐢ㄨ嚜 v1.1.4锛
public string $lastItemCssClass;

the CSS class that will be assigned to the last item in the main menu or each submenu. Defaults to null, meaning no such CSS class will be assigned.

linkLabelWrapper 灞炴 锛堝彲鐢ㄨ嚜 v1.1.4锛
public string $linkLabelWrapper;

the HTML element name that will be used to wrap the label of all menu links. For example, if this property is set as 'span', a menu item may be rendered as <li><a href="url"><span>label</span></a></li> This is useful when implementing menu items using the sliding window technique. Defaults to null, meaning no wrapper tag will be generated.

public array $submenuHtmlOptions;

HTML attributes for the submenu's container tag.

鏂规硶璇︾粏

init() 鏂规硶
public void init()
婧愮爜锛 framework/zii/widgets/CMenu.php#147 (鏄剧ず)
public function init()
{
    
$this->htmlOptions['id']=$this->getId();
    
$route=$this->getController()->getRoute();
    
$this->items=$this->normalizeItems($this->items,$route,$hasActiveChild);
}

Initializes the menu widget. This method mainly normalizes the items property. If this method is overridden, make sure the parent implementation is invoked.

isItemActive() 鏂规硶
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
婧愮爜锛 framework/zii/widgets/CMenu.php#301 (鏄剧ず)
protected function isItemActive($item,$route)
{
    if(isset(
$item['url']) && is_array($item['url']) && !strcasecmp(trim($item['url'][0],'/'),$route))
    {
        if(
count($item['url'])>1)
        {
            foreach(
array_splice($item['url'],1) as $name=>$value)
            {
                if(!isset(
$_GET[$name]) || $_GET[$name]!=$value)
                    return 
false;
            }
        }
        return 
true;
    }
    return 
false;
}

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.

normalizeItems() 鏂规硶
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
婧愮爜锛 framework/zii/widgets/CMenu.php#253 (鏄剧ず)
protected function normalizeItems($items,$route,&$active)
{
    foreach(
$items as $i=>$item)
    {
        if(isset(
$item['visible']) && !$item['visible'])
        {
            unset(
$items[$i]);
            continue;
        }
        if(!isset(
$item['label']))
            
$item['label']='';
        if(
$this->encodeLabel)
            
$items[$i]['label']=CHtml::encode($item['label']);
        
$hasActiveChild=false;
        if(isset(
$item['items']))
        {
            
$items[$i]['items']=$this->normalizeItems($item['items'],$route,$hasActiveChild);
            if(empty(
$items[$i]['items']) && $this->hideEmptyItems)
            {
                unset(
$items[$i]['items']);
                if(!isset(
$item['url']))
                {
                    unset(
$items[$i]);
                    continue;
                }
            }
        }
        if(!isset(
$item['active']))
        {
            if(
$this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item,$route))
                
$active=$items[$i]['active']=true;
            else
                
$items[$i]['active']=false;
        }
        else if(
$item['active'])
            
$active=true;
    }
    return 
array_values($items);
}

Normalizes the items property so that the 'active' state is properly identified for every menu item.

renderMenu() 鏂规硶
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'.
婧愮爜锛 framework/zii/widgets/CMenu.php#167 (鏄剧ず)
protected function renderMenu($items)
{
    if(
count($items))
    {
        echo 
CHtml::openTag('ul',$this->htmlOptions)."\n";
        
$this->renderMenuRecursive($items);
        echo 
CHtml::closeTag('ul');
    }
}

Renders the menu items.

renderMenuItem() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.6锛
protected string renderMenuItem(array $item)
$item array the menu item to be rendered. Please see items on what data might be in the item.
{return} string
婧愮爜锛 framework/zii/widgets/CMenu.php#235 (鏄剧ず)
protected function renderMenuItem($item)
{
    if(isset(
$item['url']))
    {
        
$label=$this->linkLabelWrapper===null $item['label'] : '<'.$this->linkLabelWrapper.'>'.$item['label'].'</'.$this->linkLabelWrapper.'>';
        return 
CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
    }
    else
        return 
CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
}

Renders the content of a menu item. Note that the container and the sub-menus are not rendered here.

renderMenuRecursive() 鏂规硶
protected void renderMenuRecursive(array $items)
$items array the menu items to be rendered recursively
婧愮爜锛 framework/zii/widgets/CMenu.php#181 (鏄剧ず)
protected function renderMenuRecursive($items)
{
    
$count=0;
    
$n=count($items);
    foreach(
$items as $item)
    {
        
$count++;
        
$options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
        
$class=array();
        if(
$item['active'] && $this->activeCssClass!='')
            
$class[]=$this->activeCssClass;
        if(
$count===&& $this->firstItemCssClass!==null)
            
$class[]=$this->firstItemCssClass;
        if(
$count===$n && $this->lastItemCssClass!==null)
            
$class[]=$this->lastItemCssClass;
        if(
$this->itemCssClass!==null)
            
$class[]=$this->itemCssClass;
        if(
$class!==array())
        {
            if(empty(
$options['class']))
                
$options['class']=implode(' ',$class);
            else
                
$options['class'].=' '.implode(' ',$class);
        }

        echo 
CHtml::openTag('li'$options);

        
$menu=$this->renderMenuItem($item);
        if(isset(
$this->itemTemplate) || isset($item['template']))
        {
            
$template=isset($item['template']) ? $item['template'] : $this->itemTemplate;
            echo 
strtr($template,array('{menu}'=>$menu));
        }
        else
            echo 
$menu;

        if(isset(
$item['items']) && count($item['items']))
        {
            echo 
"\n".CHtml::openTag('ul',isset($item['submenuOptions']) ? $item['submenuOptions'] : $this->submenuHtmlOptions)."\n";
            
$this->renderMenuRecursive($item['items']);
            echo 
CHtml::closeTag('ul')."\n";
        }

        echo 
CHtml::closeTag('li')."\n";
    }
}

Recursively renders the menu items.

run() 鏂规硶
public void run()
婧愮爜锛 framework/zii/widgets/CMenu.php#157 (鏄剧ず)
public function run()
{
    
$this->renderMenu($this->items);
}

Calls renderMenu to render the menu.

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.