How do I access the property of a widget

社会主义新天地 提交于 2019-12-23 17:17:57

问题


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.

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?


回答1:


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();



回答2:


Adding on the previous answer, in case you missed it, don't forget to check the short and simple official documentation on this.



来源:https://stackoverflow.com/questions/12574084/how-do-i-access-the-property-of-a-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!