Home>

smarty cache control

smarty provides powerful caching capabilities.But sometimes we don't want the entire document to be cached,Instead, a part of the content is selectively cached or the part is not cached.For example, if you use a template with a banner position at the top of the page,Advertising banner can contain any mixed information of html, image, flash, etc. So you cannot use a static link here,At the same time, we don't want the banner to be cached. This needs to be specified in the insert function,At the same time, a function is needed to get the content information of the advertising banner.Smarty also provides this cache control capability.

We can use {insert} to make part of the template not cached

You can use $smarty->register_function ($params,&$smarty) to prevent the plugin from outputting from the cache,

You can also use $smarty->register_block ($params,&$smarty) to make a block of the entire page not cached.

Now we really have a simple need,Describe the three methods of controlling the cache output.

Requirement:The current time in the cached document is not cached,Changes with each refresh.

1.Use the insert function to make part of the template not cached

index.tpl:

<div>{insert name="get_current_time"}</div>

index.php

function insert_get_current_time () {
  return date ("y-m-d h:m:s");
}
$smarty=new smarty ();
$smarty->caching=true;
if (! $smarty->is_cached ()) {
  .......
}
$smarty->display ("index.tpl");

annotation:

Define a function,The function name format is:

inser_name (array $params, object&$smarty),

Function parameters are optional,If you need to add other attributes to the insert method of the template,Will be passed to the user-defined function as an array.

Such as:

{insert name="get_current_time" local="zh"}

In the get_current_time function, we can get the property value through $params ["local"].

If you need to use the method or property of the current smarty object in the get_current_time function,It can be obtained by the second parameter.

At this point you will find that index.tpl has been cached,But the current time is constantly changing with each refresh.

2.Use register_function to prevent plugins from outputting from cache

index.tpl:

<div>{current_time} {/div}

index.php:

function smarty_function_current_time ($params,&$smarty) {
  return date ("y-m-d h:m:s");
}
$smarty=new smarty ();
$smarty->caching=true;
$smarty->register_function ("current_time", "smarty_function_current_time", false);
if (! $smarty->is_cached ()) {
  .......
}
$smarty->display ("index.tpl");

annotation:

Define a function,The function name format is:smarty_type_name ($params,&$smarty)

type is function

name is the user-defined label name,Here is {current_time}

Two parameters are required,Write it even if it is not used in a function.The functions of the two parameters are the same as above.

3.Use register_block to prevent a block in the entire page from being cached

index.tpl:

<div align="center">
page created:{"0" | date_format:"%d%h:%m:%s"}
{dynamic}
now is:{"0" | date_format:"%d%h:%m:%s"}
... do other stuff ...
{/dynamic}
</div>

index.php:

function smarty_block_dynamic ($param, $content, & $smarty) {
return $content;
}
$smarty=new smarty;
$smarty->caching=true;
$smarty->register_block ("dynamic", "smarty_block_dynamic", false);
if (! $smarty->is_cached ()) {
  .......
}
$smarty->display ("index.tpl");

annotation:

Define a function,The function name format is:smarty_type_name ($params,&$smarty)

type is block

name is the user-defined label name,Here is {dynamic}

Two parameters are required,Write it even if it is not used in a function.The functions of the two parameters are the same as above.

4.Summary

(1) the ability to control the cache:

Using register_function and register_block can easily control the buffering capacity of the plug-in output.You can control whether to cache through the third parameter,Cached by default,Requires us to display set to false, as we did in our experiments

$smarty->register_function ("current_time", "smarty_function_current_time", false);

But the insert function is not cached by default.And this property cannot be modified.In this sense, the insert function's ability to control the cache does not seem to be as strong as register_function and register_block.

(2) Convenience:

But the insert function is very convenient to use.No need to show registration,As long as this function is included in the current request, smarty will automatically find the specified function in the current request.

Of course, register_function can also prevent registration from being displayed at runtime.But the effect is the same as other template functions.Are all cached,And it cannot be controlled.

If you register a custom function with a call to register_function at runtime,Then be sure to complete the function registration before calling the is_cached () method.

Otherwise, the cached document in the is_cached () step will cause a smarty error because the registration function cannot be found

Smarty user-defined function example

<?php
$smarty->register_function ("date_now", "print_current_date");
function print_current_date ($params,&$smarty)
{
 if (empty ($params ["format"])) {
  $format="%b%e,%y";
 } else {
  $format=$params ["format"];
 }
 return strftime ($format, time ());
}
?>

Use in templates

{date_now}
{* or to format differently *}
{date_now}
php
  • Previous Method for generating static HTML from dynamic JSP page
  • Next JSP + EXt20 method to achieve paging