Many times when we use smarty, we do not want to cache the whole page, such as the real-time update information such as weather and stock. This article mainly introduces the Smarty local cache method.
Many times when we use smarty, we do not want to cache the whole page, such as the real-time update information such as weather and stock. This article mainly introduces the Smarty local cache method.
Many times when we use smarty, we do not want to cache the whole page, such as the real-time update information such as weather and stock. Therefore, we can leave it Uncached. The common implementation methods are as follows:
1. insert method:
The Insert function is similar to the inluce function. The difference is that the content contained in the insert function is not cached and will be re-executed every time the template is called. For example, we define a function to display time:
The Code is as follows:
Function insert_get_current_time (){
Return date ("H: I: s ");
}
// Then in the template:
{Insert}
In this way, each time the page is opened, the displayed time is real-time, not cached. Note that the function name must start with insert, and the name in the template corresponds to it. If our function contains some parameters, we can do this in the template:
The Code is as follows:
{Insert lid = # banner_location_id # sid = # site_id #}
// Then Smarty calls this function like insert_get_current_time (array ("lid" => "12345", "sid" => 67890 "));
The returned results are displayed at the location of the call.
2. Dynamic block Method:
In the smarty code:
The Code is as follows:
Function smarty_block_nocache ($ param, $ content, $ smarty)
{
Return $ content;
}
$ Smarty-> register_block ('nocache', 'smarty _ block_nocache', false );
In the template file:
The Code is as follows:
<{Nocache}> // content that does not need to be cached <{/nocache}>
3. Plug-in block method:
This is similar to block, but we just use it as a plug-in. Create a file: block. nocache. php In the Smarty/plugins directory. The name must be standardized. Otherwise, the smarty cannot recognize it. The content is as follows:
The Code is as follows:
Function smarty_block_nocache ($ param, $ content, $ smarty)
{
Return $ content;
}
In the template and above, you do not need to add nocache to the cache!