yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

PATCH: RFE: method dynamicly rendered widget or partial #775

Closed muayyad-alsadi closed 8 years ago

muayyad-alsadi commented 12 years ago

caching is very important, and it's very important to decrease the number of factors that let the cache varies or skipped

we used to skip full html caching (using beginCache) when a flash message is set we used to skip the cache if user is logged in (eg. to show "welcome Ahmad" box ..etc)

the solution to this is renderDynamic but its interface is not very useful

we I suggest having the following two interfaces that mimics renderPartial and renderWidget

for example we replaced

                          <?php $this->renderPartial('//layouts/_flash') ?>

with

                          <?php $this->dynamicPartial('//layouts/_flash') ?>

and we can use

                          <?php $this->dynamicPartial('//userbox/welcome_or_login') ?>
muayyad-alsadi commented 12 years ago

below is the implementation of two public methods dynamicPartial (or you might rename it to renderDynamicPartial) dynamicWidget (or you might rename it to renderDynamicWidget)

they should live in CBaseController

  /***
   * internally used method
   ***/
  public function dynamicWidgetCallback() {
    $params=func_get_args();
    $class=array_shift($params);
    $data=array_shift($params);
    return $this->widget($class,$data, true);
  }

  /***
   * renders a widget dynamically even when done between beginCache/endCache
   ***/
  public function dynamicWidget($class, $data=NULL) {
    $this->renderDynamic('dynamicWidgetCallback', $class, $data);
  }

  /***
   * internally used method
   ***/
  public function dynamicPartialCallback() {
    $params=func_get_args();
    $view=array_shift($params);
    $data=array_shift($params);
    $processOutput=array_shift($params);
    return $this->renderPartial($view, $data, true, $processOutput);
  }

  /***
   * renders a partial dynamically even when done between beginCache/endCache
   * Note: be aware of $data passed by controller
   ***/
  public function dynamicPartial($view, $data=NULL, $processOutput=false) {
    return $this->renderDynamic('dynamicPartialCallback', $view, $data, $processOutput);
  }

I'm not sure if the two internal callbacks should be protected or not

daxpresents commented 12 years ago

This solution seems a lot more intuitive than renderDynamic with a callback.

muayyad-alsadi commented 12 years ago

thank you daxpresents.

qiangxue I've not heard from you, what do think about this ?

qiangxue commented 12 years ago

renderDynamic is defined in CController, not CBaseController. I hope we can have something like $this->beginDynamic() and $this->endDynamic(), rather than wrapper of existing methods.

muayyad-alsadi commented 12 years ago

I know but I want to be able to call it inside widgets that's why I suggested to define it in CBaseController.

I hope we can have something like $this->beginDynamic() and $this->endDynamic(), rather than wrapper of existing methods.

that's a very good suggestion although I believe having a dynamic partial or widget will cover most of my needs in an intuitive way and using a small thin wrapper over a tested method

usually we have a block made as a partial like _recent_items that is reused all over the site it would still be a partial even if we do not use cache at all