When using fragment caching or page caching, we often encounter the situation where the output of the entire part is static except for a few places. For example, a help page might display static help information, but the user name displayed is that of the current user.
To solve this problem, we can match the cached content based on the username, but this will be a huge waste of our precious space, because most of the cached content is the same except for the username. We can also cut the web page into several fragments and cache them separately, but this situation will make the page and code very complicated. A better approach is to use the dynamic content feature provided by [CController].
Dynamic content means that fragment output will not be cached even if it is included in the fragment cache. Even if the included content is fetched from the cache, in order for the dynamic content to be dynamic all the time, it has to be regenerated each time. For this reason, we require dynamic content to be generated through some method or function.
Call CController::renderDynamic() to insert dynamic content where you want.
...别的HTML内容... <?php if($this->beginCache($id)) { ?> ...被缓存的片段内容... <?php $this->renderDynamic($callback); ?> ...被缓存的片段内容... <?php $this->endCache(); } ?> ...别的HTML内容...
In the above, $callback
refers to a valid PHP callback. It can be a string name pointing to a method of the current controller class or a global function. It can also be an array name pointing to a method of a class. Any other parameters will be passed to the renderDynamic() method. The callback will return dynamic content instead of just displaying it.
The above is the content of Yii Framework Official Guide Series 32 - Caching: Dynamic Content. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!