mirror of
https://github.com/smarty-php/smarty.git
synced 2025-10-17 06:25:19 +02:00
Simplified the (no)caching architecture by:
- removing support for $cache_attrs for registered plugins, - removing the undocumented {make_nocache} tag and the deprecated {insert} tag and associated code - removing support for a compile_id property on include tags. Fixes a bug in extends: resources by propagating the nocache-hashes between a master template and it's subtemplates in \Smarty\Template::_subTemplateRender. This might need further improvement.
This commit is contained in:
@@ -76,62 +76,14 @@ third parameter to [`registerPlugin()`](#api.register.plugin) is called
|
||||
|
||||
When registering a plugin with `$cacheable=false` the plugin is called
|
||||
everytime the page is displayed, even if the page comes from the cache.
|
||||
The plugin function behaves a little like an
|
||||
[`{insert}`](#plugins.inserts) function.
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> The `$cacheable` status will effect the compiled template code. If you
|
||||
> The `$cacheable` status will affect the compiled template code. If you
|
||||
> change the status you must manually delete existing compiled and
|
||||
> cached template files to force a recompile.
|
||||
|
||||
In contrast to [`{insert}`](#plugins.inserts) the attributes to the
|
||||
plugins are not cached by default. They can be declared to be cached
|
||||
with the fourth parameter `$cache_attrs`. `$cache_attrs` is an array of
|
||||
attribute-names that should be cached, so the plugin-function get value
|
||||
as it was the time the page was written to cache everytime it is fetched
|
||||
from the cache.
|
||||
|
||||
|
||||
<?php
|
||||
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
|
||||
|
||||
function remaining_seconds($params, $smarty) {
|
||||
$remain = $params['endtime'] - time();
|
||||
if($remain >= 0){
|
||||
return $remain . ' second(s)';
|
||||
}else{
|
||||
return 'done';
|
||||
}
|
||||
}
|
||||
|
||||
$smarty->registerPlugin('function','remaining', 'remaining_seconds', false, array('endtime'));
|
||||
|
||||
if (!$smarty->isCached('index.tpl')) {
|
||||
// fetch $obj from db and assign...
|
||||
$smarty->assignByRef('obj', $obj);
|
||||
}
|
||||
|
||||
$smarty->display('index.tpl');
|
||||
?>
|
||||
|
||||
|
||||
|
||||
where `index.tpl` is:
|
||||
|
||||
|
||||
Time Remaining: {remaining endtime=$obj->endtime}
|
||||
|
||||
|
||||
|
||||
The number of seconds till the endtime of `$obj` is reached changes on
|
||||
each display of the page, even if the page is cached. Since the endtime
|
||||
attribute is cached the object only has to be pulled from the database
|
||||
when page is written to the cache but not on subsequent requests of the
|
||||
page.
|
||||
|
||||
|
||||
index.php:
|
||||
Example `index.php`:
|
||||
|
||||
<?php
|
||||
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
|
||||
|
@@ -118,16 +118,13 @@ process.
|
||||
|
||||
|
||||
You can keep parts of a page dynamic (disable caching) with the
|
||||
[`{nocache}{/nocache}`](#language.function.nocache) block function, the
|
||||
[`{insert}`](#language.function.insert) function, or by using the
|
||||
[`{nocache}{/nocache}`](#language.function.nocache) block function, or by using the
|
||||
`nocache` parameter for most template functions.
|
||||
|
||||
Let\'s say the whole page can be cached except for a banner that is
|
||||
displayed down the side of the page. By using the
|
||||
[`{insert}`](#language.function.insert) function for the banner, you can
|
||||
keep this element dynamic within the cached content. See the
|
||||
documentation on [`{insert}`](#language.function.insert) for more
|
||||
details and examples.
|
||||
displayed down the side of the page. By using a [`{nocache}{/nocache}`](#language.function.nocache)
|
||||
block for the banner, you can
|
||||
keep this element dynamic within the cached content.
|
||||
|
||||
You can clear all the cache files with the
|
||||
[`clearAllCache()`](#api.clear.all.cache) function, or individual cache
|
||||
|
Reference in New Issue
Block a user