To invalidate cache in certain areas (only the cache that is needed), there are several ways:
1. inser:
Define a processing function to be used by the insert tag. The function name format is: insert_xx (array $params, object &$smarty) where xx is the name of insert. That is to say, if the function you define is insert_abc, then The method used in the template is {insert name=abc}
Parameters are passed in through $params
It can also be made into an insert plug-in. The file name is: insert.xx.php, the function is named: smarty_insert_aa($params,&$smarty), and the definition of xx is the same as above
2. register_block:
Define a block:smarty_block_name($params,$content, &$smarty){return $content;} //name represents the area name
Register block:$smarty->register_block(name, smarty_block_name, false); //The third parameter false means that the area is not cached
Template writing: {name} content {/name}
Written as block plug-in:
(1) Define a plug-in function: block.cacheless.php and place it in smarty’s plugins directory
The content of block.cacheless.php is as follows:
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
(2) Write programs and templates
Sample program: testCacheLess.php
include(Smarty.class.php);
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display(cache.tpl);
?>
Template used: cache.tpl
Cached: {$smarty.now}
{cacheless}
No cache: {$smarty.now}
{/cacheless}
Run it now and find that it doesn’t work. Both lines of content are cached
(3) Rewrite Smarty_Compiler.class.php (Note: This file is very important, please back it up first to restore it if necessary)
Find$this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true); //Mine is at line 705
was changed to:
if($tag_command == cacheless) $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, false);
else $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);
You can also directly change the last parameter of the original sentence to false. I don’t know much about the internal mechanism of smarty, so I added a judgment. As long as the block is cacheless, it will not be cached
(4) OK, now clear the compiled files in template_c and run it again. Does it work?