Is There A Way To Dynamically Render A Widget?

Hello,

i’m developing for some time with Yii now, and now i got a new challange in developing with Yii :)

I want to cache whole pages with the COutputCache filter. This works fine, with any cache component. Now i need to use a widget in my main layout and some single views, but this widget has to return different values for different devices (e.g. for desktop, tablet or smartphone) AND different top level domains (e.g. .com, .de, .nl, in total 11 top level domains).

Currently i set an varyByExpression for the COutputFilter, which in my opinion is a bad workaround because there can be (actually) 44 different caches for one site.

I tried to use renderDynamic in the widget from the calling controller but it doesn’t work.

Here is one example which i tried. The result in this case is that the random number is cached too:

layouts/main.php:




...some code...

<?php $this->widget('randomNumber'); ?>

...some code...



components/ownController.php:




...some code...

public function renderDynamicContentFullFilePath($viewFile, $data = null) {

 return $this->renderFile($viewFile, $data, true);

}

...some code...



components/randomNumberWidget.php:




...some code...

public function run() {

 $randomNumber = mt_rand();

 return $this->controller->renderDynamic('renderDynamicContentFullFilePath', $this->getViewFile('randomNumber'), array('randomNumber' => $randomNumber));

...some code...



components/views/randomNumber.php:




<?php echo $randomNumber; ?>



I tried same example code with an subtemplate which is renderd in the layout with an renderDynamic surrounding it. This works, but i find this is blowing the whole code up.

Is there any other solution that works without a new subtemplate?

I hope anybody can help me :)

Dirk

Actually I did not understand why you need method


ownController::renderDynamicContentFullFilePath

.

Solution is rather simple.

layouts/main.php:




...some code...

<?php $this->renderDynamic(

    'widget',

    'randomNumber',

    true

); ?>

...some code...



components/randomNumber.php:




...some code...

public function run() {

    $randomNumber = mt_rand();

    echo $randomNumber;

}

...some code...