I am using Widgets within Widgets so needed to capture the output from the nested Widgets to add to the output in the parent.
I came across this post http://www.yiiframew…26.html#msg3826, but Clips didn’t seem the right way to go as the output from the nested Widget was used once only, so saving wasn’t necessary.
I arrived at the following solution and hope it might be useful to others; I've called it Element:
class EElement extends CWidget { private $content; public function __get($name) { if ($name === 'content') return $this->content; else return parent::__get($name); } public function render($view, $data) { $this->content = parent::render($view, $data, true); } }
The EElement class extends CWidget, provides the $content property to capture the rendering result, and overrides the render() method to ensure that the rendering result is returned.
An Element looks just like a Widget except it extends the EElement class:
class MyElement extends EElement { public $attribute; public function run() { $this->render('view', array('attribute'=>$this->attribute)); } }
Where the content of the Element is required it is called in exactly the same way as a widget; you then access the content property to get the result:
echo ($this->widget('path.to.element', array('attribute' => $value))->content);