Smarty template cache Tutorial

Source: Internet
Author: User
First of all, there are two different concepts about smarty caching and compilation. by default, the compilation is started, and the caching mechanism needs to be manually enabled. the files compiled by smarty are still php files, therefore, it is compiled during execution. if the database is involved, you still need to access the database.

First of all, there are two different concepts about smarty caching and compilation. by default, the compilation is started, and the caching mechanism needs to be manually enabled. the files compiled by smarty are still php files, therefore, it is compiled during execution. if the database is involved, it is still necessary to access the database, so the cost is not small, so the smarty cache is needed to solve the problem!

1. enable global cache

$ Smarty-> cache_dir = "/caches/"; // cache Directory
$ Smarty-> caching = true; // When cache is enabled, the cache is invalid.
$ Smarty-> cache_lifetime = 3600; // cache time

 

2. one page uses multiple caches

For example, an article template page generates multiple article pages. of course, the page is cached as many pages, which is easy to implement. you only need to set the second parameter in the display () method and specify a unique identifier. The following php code:

$ Smarty-> display ('index. tpl ', $ _ GET ["article_id"]);

As shown above, an article page is cached by the id of the second parameter article.

 

3. Reduce overhead for cache

That is to say, the cached page does not need to be processed by database operations. you can use the is_cached () method to determine the operation!

If (! $ Smarty-> is_cached ('index. tpl ')){

// Call the database

}

$ Smarty-> display ('index. tpl ');

 

4. clear cache

Generally, cache is not enabled during the development process, because the output results remain unchanged during the cache time, but enabling the cache during the application process can greatly improve web performance. The Cache clearing method is as follows:

Clear_all_cache (); // clear all caches
Clear_cache ('index. tpl '); // clear the cache of index. tpl
Clear_cache ('index. tpl ', cache_id); // clear the cache with the specified id

 

5. disable local cache

If one part of a page is cached, and the other part does not need to be cached, you can do so. for example, to display the user login name, you need to disable the cache. smarty provides the following three solutions:

(1) using part of the insert template is not cached

Defines the handler to be used for an inser label. the function name format is: insert_xx (array $ params, object & $ smarty). xx indicates the name of insert, that is, if the function you define is insert_abc, the method used in the template is {insert name = abc}

The parameter is passed in through $ params

You can also create an insert plug-in. the file name is insert. xx. php, and the function name is smarty_insert_aa ($ params, & $ smarty). xx is defined as the same as above.

(2) $ smarty-> register_block ($ params, & $ smarty) prevents a part of the entire page from being cached

Define a block: smarty_block_name ($ params, $ content, & $ smarty) {return $ content;} // name indicates the region name

Register block: $ smarty-> register_block (name, smarty_block_name, false); // The third parameter false indicates that the region is not cached.

Template syntax: {name} content {/name}

Write the block plug-in:

Step 1: define a plug-in function: block. cacheless. php, which is placed in the smarty plugins directory.

The contents of block. cacheless. php are as follows:

Function smarty_block_cacheless ($ param, $ content, & $ smarty ){

Return $ content;

}

?>

Part 2: Programming and templates

Example: testCacheLess. php

Include (Smarty. class. php );

$ Smarty = new Smarty;

$ Smarty-> caching = true;

$ Smarty-> cache_lifetime = 6;

$ Smarty-> display (cache. tpl );

?>

Template used: cache. tpl

Cached: {$ smarty. now}

{Cacheless}

No cache: {$ smarty. now}

{/Cacheless}

Run it now and find that it does not work, and the two lines of content are cached.

Step 3: rewrite Smarty_Compiler.class.php (note: This file is very important. back up the file first to recover it if necessary)

Find $ this-> _ plugins [block] [$ tag_command] = array ($ plugin_func, null, true );

Modify:

If ($ tag_command = cacheless) $ this-> _ plugins [block] [$ tag_command] = array ($ plugin_func, null, false );

Else $ this-> _ plugins [block] [$ tag_command] = array ($ plugin_func, null, true );

You can also change the last parameter of the original sentence to false to disable the default cache.

(3) use register_function to prevent the plug-in from outputting data from the cache.

Index. tpl:
{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.

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.