Php smarty cache application and control

Source: Internet
Author: User
Tags current time php file php tutorial

Php Tutorial: smarty cache application and control

First look at a small program

Configure the php environment in advance. We recommend that you install xampp.
 

1. Create a project directory

Create the following directory in the directory:


2. Upload All files under the libs directory in the smarty package to the "smarty" Directory of the new project directory.

3. Create a new test. Php file with the following content:
 

Code
<? Php
Require 'smarty/smarty. class. Php ';
$ Smarty = new smarty;
$ Smarty-> compile_check = true;
$ Smarty-> debugging = true;
$ Smarty-> left_delimiter = "<{";
$ Smarty-> right_delimiter = "}> ";
$ Smarty-> assign ("name", "elar ");
$ Smarty-> assign ("gender", "female ");
$ Smarty-> assign ("age", "21 ");
$ Smarty-> display ("test. tpl ");
?>

4. Create a "test. tpl" file under the templates Directory. The content is as follows:
 

1 name is <{$ name}> <br/> 2 age is <{$ age}> <br/> 3 gender is <{$ gender}> <br/>

5. Run in a browser
 

Http: // localhost/mytestsmarty/test. php

 

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. The format of the function name is inser_name (array $ params, object & $ smarty). The Function parameter is optional. If other attributes need to be added to the insert method of the template, it is passed to the user-defined function as an array.

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 the experiment, "$ 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.

 

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.