Smarty data cache and template caching

Source: Internet
Author: User
Smarty data cache and template caching

What is a template cache?

Smarty will replace the template written in the Smarty syntax with PHP format for PHP parsing, PHP data and interface separation.

When we modify the template file every time, the corresponding template cache will be regenerated once.

But this is just the template cache, the program still need to get data from the database and operation, and directly in PHP written out of the interface without any difference.

How do I implement a true data cache?

Smarty supports true data caching, that is, conditional compliance will return a cached static file directly to the browser and will not be retrieved from the database.

You need to add the following settings:

$smarty->caching = true;//Open Cache $smarty->cache_dir = ' ... /cache/';//cache directory, customizable

When we run the program, we will find the cache directory to generate some HTML files, open can be seen are static HTML page

Local cache

The Insert function is not cached by default. And this property cannot be modified.

Cache1.htm{insert name= "MyTime"}cache1.phpfunction insert_mytime () {        return date ("y-m-d h:i:s");}


The Smarty_block function can also implement local caching

{Blockname} current time: {$smarty. Now}{/blockname}

Add an ID number to the cache and a template to implement multiple caches

$smarty->display (' template file ', cache ID); Create a cache with ID $smarty->clear_all_cache (); Clears all cache $smarty->clear_cache (' template files ');//Clears the cache $smarty->clear_cache (' template file ', cache ID) of the specified template file;//clears the cache for the specified ID


For example, you can generate different cache pages based on the ID number:

$id = "id=". $_get[' id '); $smarty->display ("Index.tpl", $id);
  • 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.