Introduction to several methods of Smarty local cache

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.