Smarty local cache technology [Source Code Analysis]

Source: Internet
Author: User

I have been using other template engines all the time. When I was attending bkJia training courses today, I spoke about the partial cache of the smarty template engine. I feel pretty good. I'm so impressed with my understanding here, if something is wrong, I hope my friend will reply with me. We will encourage you to learn from each other. At the same time, I would like to thank Mr. Zhang for providing a very good learning platform for PHP beginners like ours.

I found that smarty is so powerful and magical that I like it as much as the template engine I used previously.

Because I enable the smarty cache by default, but in some places the data is updated in real time or updated quickly, it is not suitable for caching, so that local cache will be useful.

1. insert Method

Defines the display time of a function:

 
 
  1. function insert_get_current_time(){  
  2.     $timestamp=emptyempty($timestamp)?time():$timestamp;  
  3.     $timeoffset=(int) +8;  
  4.     return $ret=gmdate("Y-n-j g:ia", $timestamp + $timeoffset * 3600);  

Then in the template:

 

 
 
  1. {insert name="get_current_time"} 

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.

This method is simple, but it is not suitable if the content to be displayed is large.

2. Dynamic block Method

 

 
 
  1. // Partial Cache
  2. Function smarty_block_nocache ($ param, $ content, $ smarty)
  3. {
  4. Return $ content;
  5. }
  6. $ Smarty-> register_block (nocache, smarty_block_nocache, false );

In the template:

 

 
 
  1. {nocache}  
  2. {$smarty.now}  
  3. {/nocache} 

In this way, the time displayed for each page refresh is different.

3. Plug-in block Method

Create an object in the Smarty/plugins directory

The content of block. nocache. php is as follows:

 

 
 
  1. <?php   
  2. function smarty_block_nocache($param, $content, $smarty)  
  3. {  
  4.       return $content;   
  5. }    
  6. ?> 

This is the same as method 2, and the labels in the template are the same. Register_block is unnecessary in the PHP file, which is very convenient.

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.