如何访问小部件的属性 [英] How do I access the property of a widget
问题描述
我是 Yii 框架的新手,我正在尝试访问小部件的属性,比如说 CMenu,我想在其中更改一些公共属性的值,例如 activeCssClass、firstItemCssClass、lastItemCssClass、htmlOptions 等.所以你如何在 Yii 中更改小部件的属性.
I'm new to the Yii framework and I'm trying to access a property of the of a widget let's say CMenu where I want to change the values of some public properties like activeCssClass, firstItemCssClass, lastItemCssClass, htmlOptions etc. so how do you change the property of a widget in Yii.
详情:我使用的是 Yii 版本 1.1.12(2012 年 8 月 19 日),我正在尝试生成一个多级菜单,但我需要更改一些公共类参数的值,但我不知道如何更改?
Details: I'm using Yii version 1.1.12 (Aug 19, 2012) and I'm trying to generate a multilevel menu but I need to change the values of some public class parameters and I don't know how?
推荐答案
好吧,通常您在调用小部件时应用所需的值.您可以在小部件类名称之后的小部件调用中将它们设置在适当的数组中.
Well, normally you apply needed values when you call the widget. You set them at the appropriate array inside widget call after widget class name.
$this->widget('zii.widgets.CMenu',
array(
'items' => $items,
'id' => 'main_menu',
'htmlOptions' => array('class' => 'nav'),
'activeCssClass' => 'active',
'firstItemCssClass' => 'first_item'
)
);
但是!如果您想在创建一些小部件后应用这些值,但尚未呈现它(非常罕见的情况),您可以执行以下操作:
BUT! If you want to apply the values after you created some widget, but have not rendered it yet (really rare case) you can do this thing:
$widget = $this->beginWidget('application.components.MyOwnWidget');
$widget->public_property = 'aaa';
$widget->renderSomething();
$this->endWidget();
这篇关于如何访问小部件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!