Brief introduction of several methods of Smarty local cache

  • 2021-07-01 06:57:06
  • OfStack

Many times when we use smarty, we don't want to cache the whole page, such as weather, stocks and other instantly updated information. Then we can make it uncached. There are three common ways to implement it:

1. insert method:

The Insert function is similar to the inluce function except that the contents of insert are not cached and the function is re-executed every time the template is called. For example, let's define a function that displays time:

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

In this way, every time a page is opened, the immediate time is displayed instead of the cached time. Note that the function name 1 must start with insert, and name in the template corresponds to it. If our function contains 1 arguments, then our template can be like this:

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

And displays the returned result at the calling location.

2. Dynamic block method:

In the smarty code:

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

In the template file:

<{nocache}>    // Put content that doesn't need to be cached here  <{/nocache}> 

3. Plug-in block method:

This is similar to block, except that we do it in the form of plug-ins. In the Smarty/plugins directory to build a file: block. nocache. php, here named 1 must be standard, otherwise smarty can not recognize. The contents are as follows:

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

Add nocache in the template and above 1 straight, where it doesn't need to be cached!

Related articles: