PHP file Cache smarty Template Application example analysis, Smarty application Example _php tutorial

Source: Internet
Author: User
Tags php template smarty template

PHP file Cache smarty Template Application example analysis, Smarty application Example


This paper analyzes the application of PHP file cache smarty template. Share to everyone for your reference, as follows:

First, use the cache

To turn on the Smarty cache, simply set caching to True and specify Cache_dir.
Use Cache_lefetime to specify the cache lifetime in seconds
To generate multiple different caches for the same page, add a second parameter, cache_id, to the display or fetch, such as:

$smarty->display (' Index.tpl ', $my _cache_id);

This attribute can be used to cache different $_get

Second, clear the cache

Clear_all_cache ();//Clear All cache Clear_cache (' index.tpl ');//Clear Index.tpl Cache Clear_cache (' Index.tpl ', cache_id);// Clears the cache for the specified ID

Iii. using a custom cache method

Set Cache_handler_func to handle caching using a custom function
Such as:

$smarty->cache_handler_func = "Mycache"; function Mycache ($action, & $smarty _obj, & $cache _content, $tpl _ File=null, $cache _id=null, $compile _id=null) {}

The function is generally rooted in the $action to determine the current operation of the cache:

Switch ($action) {case "read"://Read Cache contents case "write"://write Cache case "clear"://Empty}

General use of MD5 ($TPL _file. $cache _id. $compile _id) as the only cache_id
If required, use gzcompress and gzuncompress to compress and decompress

Four, partial shutdown cache

There are several ways to invalidate the cache in some areas (only for the desired cache):

Insert

Defines the handler function to use for an insert label, with the format of the function name: insert_xx (array $params, Object & $smarty) where xx is the name of the insert, that is, if you define a function as Insert_ ABC, the method used in the template is {insert name= ' abc '}

Parameters passed through $params

can also be made into insert plug-in, file name named: insert.xx.php, Function named: Smarty_insert_aa ($params,& $smarty), xx definition ibid.

Register_block:

Define a block:

Smarty_block_name ($params, $content, & $smarty) {return $content;} Name indicates the area domain name

Register BLOCK:

$smarty->register_block (' name ', ' Smarty_block_name ', false);//The third parameter false indicates that the zone is not cached

Template notation:

{Name} content {/name}

Written as Block plugin:

1) Define a plug-in function: block.cacheless.php, placed in the Smarty plugins directory
The contents of block.cacheless.php are as follows:

<?phpfunction smarty_block_cacheless ($param, $content, & $smarty) {   return $content;}? >

2) Writing Programs and templates

Example program: testcacheless.php

<?phpinclude (' Smarty.class.php '); $smarty = new Smarty; $smarty->caching=true; $smarty->cache_lifetime = 6;$ Smarty->display (' Cache.tpl ');? >

Template used: CACHE.TPL

Already cached: {$smarty. Now}
{cacheless} is not cached: {$smarty. now}{/cacheless}

More readers interested in PHP related content can view this site topic: "Smarty Template Primer Basic Tutorial", "PHP date and Time usage summary", "PHP Object-oriented Programming primer tutorial", "PHP string (String) Usage Summary", "php+ MySQL database operation Getting Started tutorial and PHP Common database operation Skills Summary

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP implementation of Smarty template infinite Pole classification method
    • Smarty Template Condition Judgment usage example in PHP
    • Smarty how the template engine obtains data from PHP
    • PHP smarty Template Usage Example detailed
    • thinkphp How to use the Smarty template engine
    • 6 Tips for PHP smarty template engine
    • PHP based on the method of using Smarty template in Yii framework
    • Smarty Template first experience in PHP
    • Ways to use PHP functions in Smarty Templates
    • PHP Smarty Template method for generating HTML documents
    • Summary of cache Usage for PHP template engine Smarty
    • Caching applications in the PHP smarty template engine

http://www.bkjia.com/PHPjc/1104332.html www.bkjia.com true http://www.bkjia.com/PHPjc/1104332.html techarticle php File Cache smarty Template Application example analysis, Smarty application Example this paper analyzes the PHP file cache smarty Template application. Share to everyone for your reference, as follows: First, make ...

  • 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.