Home > Backend Development > PHP Tutorial > Introduction to several methods of Smarty partial caching_PHP tutorial

Introduction to several methods of Smarty partial caching_PHP tutorial

WBOY
Release: 2016-07-13 10:24:42
Original
852 people have browsed it

Many times when we use smarty, we do not want to cache the entire page, such as weather, stocks and other information that is updated in real time. Then we can make it not cached. Common implementation methods include the following three methods:

1.insert method:

The Insert function is similar to the inluce function, except that the content contained in insert will not be cached, and the function will be re-executed every time the template is called. For example, let’s define a function that displays time:

Copy code The code is as follows:

function insert_get_current_time() {
return date("H:i:s" );
}
//Then in the template:
{insert name="get_current_time"}

In this way, every time you open the page, the real-time time will be displayed instead of the cached time. Note that the function name here must start with insert, and the name in the template corresponds to it. If our function contains some parameters, then our template can be like this:

Copy code The code is as follows:

{insert name="get_current_time" lid=#banner_location_id# sid=#site_id#}
//Then Smarty calls this function similar to insert_get_current_time(array("lid"=>"12345","sid"=>67890"));

and returns the result Displayed at the calling location

2. Dynamic block method:

In smarty code:

Copy code The code is as follows:

function smarty_block_nocache($param,$content,$smarty)
{
return $content;
}
$smarty->register_block('nocache','smarty_block_nocache',false);

In template file:

Copy code The code is as follows:

<{nocache}> //Put content that does not need to be cached here< ;{/nocache}>

3. Plug-in block method:

This is similar to block, but we do it in the form of a plug-in. Create a file in the Smarty/plugins directory: block.nocache.php. The naming here must be standardized, otherwise smarty will not recognize it. The content is as follows:

Copy code The code is as follows:

function smarty_block_nocache($param,$content,$smarty)
{
return $content;
}

In the template and above, just add nocache to places that do not need to be cached!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825341.htmlTechArticleMany times when we use smarty, we do not want to cache the entire page, such as real-time updates such as weather, stocks, etc. information. Then we can make it not cached. Common implementations are as follows...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template