Analysis of cache operation skills for Smarty advanced applications and smarty cache Operation Skills

Source: Internet
Author: User

Analysis of cache operation skills for Smarty advanced applications and smarty cache Operation Skills

This article describes the cache operations of Smarty advanced applications. We will share this with you for your reference. The details are as follows:

Smarty Cache Control

Smarty provides powerful caching functions. However, sometimes we do not want the entire document to be cached, but choose to cache a part of the content or a part of the content. For example, if you use a template with the ad bar position on the top of the page, the ad bar can contain any HTML, image, FLASH, and other mixed information. therefore, a static link cannot be used here, and we do not want the ad bar to be cached. this requires the insert function to specify and a function to retrieve the content of the advertisement bar. 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 plug-in from outputting data from the cache,

You can also use $ smarty-> register_block ($ params, & $ smarty) to prevent a part of the entire page from being cached.

The following describes the three methods to control the cache output for a simple requirement.

Requirement: the cached document is not cached at the current time and 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');

Note:

Define a function in the format:

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

Function parameters are optional. If other attributes need to be added to the insert method of the template, they will be passed as an array to the User-Defined Function.

For example:

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

In the get_current_time function, we can use $ params ['local'] to obtain the attribute value.

If the method or attribute of the current smarty object needs to be used in the get_current_time function, it can be obtained through the second parameter.

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

2. Use register_function to prevent the plug-in from outputting data from the 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');

Note:

Define a function in the format of smarty_type_name ($ params, & $ smarty)
Type is function

Name is the name of the custom tag. Here is {current_time}

The two parameters are required, even if they are not used in the function. The functions of the two parameters are the same as those of the preceding two parameters.

3. Use register_block to prevent a part of 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');

Note:

Define a function in the format of smarty_type_name ($ params, & $ smarty)
Type is block

Name is the name of the custom tag. Here is {dynamic}

The two parameters are required, even if they are not used in the function. The functions of the two parameters are the same as those of the preceding two parameters.

4. Summary

(1) Cache control capabilities:

You can use register_function and register_block to conveniently control the buffer capacity of the plug-in output. You can use the third parameter to control whether the plug-in is cached. The default value is cached. You need to set the value to false, as we did in our experimentsCopy codeThe Code is as follows: $ smarty-> register_function ('current _ time', 'smarty _ function_current_time ', false );
However, the insert function is not cached by default. This attribute cannot be modified. In this sense, the insert function does not seem to have better control over the cache than register_function and register_block.

(2) ease of use:

However, the insert function is very convenient to use. You do not need to display the registration. As long as this function is included in the current request process, smarty will automatically find the specified function in the current request process.

Of course, register_function can also display registration when it is not running. However, the effects of such operations are the same as those of other template functions. They are all cached and cannot be controlled.

If you use the display when running to call register_function to register a UDF, you must complete the function registration before calling the is_cached () method.

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

Smarty User-Defined Function instance

<?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 the template

{date_now}{* or to format differently *}{date_now format="%Y/%m/%d"}

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.