mirror of
https://github.com/yiisoft/yii.git
synced 2026-03-04 23:34:05 +01:00
45 lines
1.9 KiB
Plaintext
45 lines
1.9 KiB
Plaintext
Dynamic Content
|
|
===============
|
|
|
|
When using [fragment caching](/doc/guide/caching.fragment) or [page
|
|
caching](/doc/guide/caching.page), we often encounter the situation where
|
|
the whole portion of the output is relatively static except at one or
|
|
several places. For example, a help page may display static help
|
|
information with the name of the user currently logged in displayed at the
|
|
top.
|
|
|
|
To solve this issue, we can variate the cache content according to the
|
|
username, but this would be a big waste of our precious cache space since
|
|
most content are the same except the username. We can also divide the page
|
|
into several fragments and cache them individually, but this complicates
|
|
our view and makes our code very complex. A better approach is to use the
|
|
*dynamic content* feature provided by [CController].
|
|
|
|
A dynamic content means a fragment of output that should not be cached
|
|
even if it is enclosed within a fragment cache. To make the content dynamic
|
|
all the time, it has to be generated every time even when the enclosing
|
|
content is being served from cache. For this reason, we require that
|
|
dynamic content be generated by some method or function.
|
|
|
|
We call [CController::renderDynamic()] to insert dynamic content at the
|
|
desired place.
|
|
|
|
~~~
|
|
[php]
|
|
...other HTML content...
|
|
<?php if($this->beginCache($id)) { ?>
|
|
...fragment content to be cached...
|
|
<?php $this->renderDynamic($callback); ?>
|
|
...fragment content to be cached...
|
|
<?php $this->endCache(); } ?>
|
|
...other HTML content...
|
|
~~~
|
|
|
|
In the above, `$callback` refers to a valid PHP callback. It can be a
|
|
string referring to the name of a method in the current controller class or
|
|
a global function. It can also be an array referring to a class method. Any
|
|
additional parameters to [renderDynamic()|CController::renderDynamic()]
|
|
will be passed to the callback. The callback should return the dynamic
|
|
content instead of displaying it.
|
|
|
|
<div class="revision">$Id$</div> |