Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii passing parameters to widget

Sorry if I am using wrong keyword. Might be I did not get sufficient information due to wrong keyword.

we create widget in Yii by this way:

class streamList extends CWidget {
  //do some stuff
}

Also, we use this widget anywhere as

$this->widget("application.components.widget.streamList");

How can we write the widget in such a way that it accepts parameter(s) as

$this->widget("application.components.widget.streamList",array('title'=>'Posts');

I googled but did not solve. Please help, thank in advance.

Edit log: Also, how can we define default parameter value to 'titile'? I tried public $title = Shk::getTitle(), but it did not work.

like image 268
Shahid Karimi Avatar asked Dec 15 '22 14:12

Shahid Karimi


1 Answers

Use

class StreamList extends CWidget {
    //do some stuff
    public $title;
}

Any attributes can be initialized with default values and overwritten by

$this->widget("application.components.widget.StreamList",array('title'=>'Posts',.....)

EDIT

You can't initialize class attributes with functions. An explanation is given here. An option is to check whether $title is set and if not set it to Shk::getTitle() in the init() method like

public function init(){
    ....
    if(!isset($this->title) || !$this->title)
        $this->title=Shk::getTitle();
    ....
}

P.S for consistency it's better to Capitalize your class names.

like image 190
topher Avatar answered Feb 20 '23 03:02

topher