Smarty Advanced Application cache operation skills Analysis, Smarty caching tips
The examples in this paper describe the caching techniques of smarty advanced applications. Share to everyone for your reference, as follows:
Smarty Cache Control
The Smarty provides powerful caching capabilities. But sometimes we don't want the entire document to be cached, but we have the choice to cache a portion of the content or a portion of the content that is not cached. For example, if you use a template with a banner location at the top of the page, the banner can contain any mix of HTML, images, Flash, and so on. So there's no way to use a static link, and we don't want the banner to be cached. This needs to be specified in the Insert function and requires a function to take the content information of the ad bar. The Smarty also provides this cache control capability.
We can use {Insert} to make part of a template not cached
You can use $smarty->register_function ($params,& $smarty) to block plug-ins from being output from the cache.
You can also use $smarty->register_block ($params,& $smarty) to make a piece of the entire page not be cached.
Below we really have a simple need to explain the three ways to control the output of the cache.
Requirement: The current time in the cached document is not cached and varies with each refresh.
1. Use the Insert function to make part of the template not cached
INDEX.TPL:
{Insert Name= "Get_current_time"}
index.php
function Insert_get_current_time () { return date ("y-m-d h:m:s");} $smarty =new smarty (); $smarty->caching = true;if (! $smarty->is_cached ()) { ...} $smarty->display (' Index.tpl ');
Annotations:
Define a function with the format of the function name:
Inser_name (array $params, Object & $smarty),
Function arguments are optional, and if additional attributes are required in the Insert method of the template, they are passed to the user-defined function as an array.
Such as:
{Insert Name= ' get_current_time ' local= ' zh '}
In the Get_current_time function we can get the value of the property by $params[' local '.
If the method or property of the current Smarty object needs to be used in the Get_current_time function, it can be obtained by the second parameter.
At this point you will find that the INDEX.TPL has been cached, but the current time is changing with each refresh.
2. Use Register_function to prevent plugins from being exported from the cache
INDEX.TPL:
{Current_time} {/div}
index.php:
function Smarty_function_current_time ($params, & $smarty) { return date ("y-m-d h:m:s");} $smarty =new smarty (); $smarty->caching = true; $smarty->register_function (' current_time ', ' Smarty_function_ Current_time ', false); if (! $smarty->is_cached ()) { ...} $smarty->display (' Index.tpl ');
Annotations:
Defines a function with the format of the function name:smarty_type_name ($params, & $smarty)
Type is function
Name is the user-defined label name, here is {current_time}
Two parameters are required, even if they are not used in a function. The function of two parameters is the same as above.
3, using Register_block to make a piece of the whole page is not cached
INDEX.TPL:
Page created: {"0" |date_format: "%d%h:%m:%s"}{dynamic}now is: {"0" |date_format: "%d%h:%m:%s"} ... do other stuff ... {/dynamic}
index.php:
function smarty_block_dynamic ($param, $content, & $smarty) {return $content;} $smarty = new Smarty; $smarty->caching = true; $smarty->register_block (' Dynamic ', ' smarty_block_dynamic ', false); if (! $smarty->is_cached ()) { ...} $smarty->display (' Index.tpl ');
Annotations:
Defines a function with the format of the function name:smarty_type_name ($params, & $smarty)
Type is block
Name is the user-defined label name, here is {dynamic}
Two parameters are required, even if they are not used in a function. The function of two parameters is the same as above.
4. Summary
(1) The ability to control the cache:
Using Register_function and Register_block can easily control the buffer capacity of the plug-in output, you can control whether the cache by the third parameter, the default is cached, we need to display set to False, as we have done in the experiment to copy the code the code is as follows: $smarty->register_function (' current_time ', ' smarty_function_current_time ', false);
However, the Insert function is not cached by default. And this property cannot be modified. In this sense, the Insert function does not seem to have the ability to control the cache as Register_function and Register_block.
(2) Ease of use:
However, the Insert function is very convenient to use. No registration is displayed, as long as the function is included in the current request Smarty will automatically look for the specified function during the current request.
Of course register_function can also do not display registration at run time. But the effect of doing that is like any other template function, which is cached and cannot be controlled.
If you use the run-time display call Register_function to register a custom function, be sure to complete the function registration before calling the Is_cached () method.
Otherwise, caching the document in Is_cached () will cause a smarty error because the registration function could not be found
Smarty user-defined function instances
<?php$smarty->register_function (' Date_now ', ' print_current_date '); function print_current_date ($params, & $smarty) {if ($params [' format ']) { $format = "%b%e,%Y";} else { $format = $params [' Format '];} retur N Strftime ($format, Time ());}? >
Use in templates
{Date_now} {* or to format differently *} {Date_now format= "%y/%m/%d"}
More interested in smarty related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary
It is hoped that this article will be helpful to everyone based on smarty template PHP program design.
http://www.bkjia.com/PHPjc/1127907.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127907.html techarticle Smarty Advanced Application cache operation skills Analysis, Smarty cache operation Tips This article describes the caching techniques for smarty advanced applications. Share to everyone for your reference, as follows: ...