Nested Widgets - an idea

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('', array('attribute' => $value))->content);

The third parameter of the render method is a boolean which determines if the rendered view should be returned or echoed. See http://www.yiiframew…r#render-detail for more details.

I do not understand why you would do it. You can simply you another widget in the view…

some text in the widget view

<?php $this->widget('some.other.widget') ?>