PHP file caching smarty template application example analysis, smarty application example_PHP tutorial

WBOY
Release: 2016-07-12 08:58:14
Original
768 people have browsed it

PHP file caching smarty template application example analysis, smarty application example

This article analyzes the PHP file caching smarty template application example. Share it with everyone for your reference, the details are as follows:

1. Use cache

To enable smarty’s cache, just 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 the second parameter cache_id to display or fetch, such as:

$smarty->display('index.tpl',$my_cache_id);

Copy after login

This feature can be used to cache different $_GETs differently

2. Clear cache

clear_all_cache();//清除所有缓存
clear_cache('index.tpl');//清除index.tpl的缓存
clear_cache('index.tpl',cache_id);//清除指定id的缓存

Copy after login

3. Use custom caching method

Set cache_handler_func to use a custom function to handle cache
Such as:

$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}

Copy after login

This function generally determines the current operation of the cache based on $action:

switch($action){
case "read"://读取缓存内容
case "write"://写入缓存
case "clear"://清空
}

Copy after login

Generally use md5 ($tpl_file.$cache_id.$compile_id) as the only cache_id
If necessary, use gzcompress and gzuncompress to compress and decompress

4. Partially close the cache

To invalidate cache in certain areas (only the required cache), there are several ways:

insert:

Define a processing function to be used by the insert tag. The function name format is: insert_xx (array $params, object &$smarty) where xx is the name of insert. That is to say, if the function you define is insert_abc, then The method used in the template is {insert name='abc'}

Parameters are passed in through $params

It can also be made into an insert plug-in. The file name is: insert.xx.php, the function is named: smarty_insert_aa($params,&$smarty), and the definition of xx is the same as above

register_block:

Define a block:

smarty_block_name($params,$content, &$smarty){return $content;}
//name表示区域名

Copy after login

Registration block:

$smarty->register_block('name', 'smarty_block_name', false);
//第三参数false表示该区域不被缓存

Copy after login

Template writing:

{name}内容{/name}

Copy after login

Written as block plug-in:

1) Define a plug-in function: block.cacheless.php and place it in smarty’s plugins directory
The content of block.cacheless.php is as follows:

<&#63;php
function smarty_block_cacheless($param, $content, &$smarty) {
   return $content;
}
&#63;>
Copy after login

2) Write programs and templates

Sample program: testCacheLess.php

<&#63;php
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display('cache.tpl');
&#63;>

Copy after login

Template used: cache.tpl

已经缓存的:

{$smarty.now}<br>
{cacheless}

 
没有缓存的:

{$smarty.now}
{/cacheless}

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Basic Tutorial for Getting Started with Smarty Templates", "Summary of PHP Date and Time Usage", "Introduction Tutorial on PHP Object-Oriented Programming", "php String (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • How to implement infinite classification of smarty templates in php
  • Examples of conditional judgment usage of smarty templates in php
  • smarty template How the engine obtains data from php
  • Detailed explanation of examples of how to use Smarty template in php
  • ThinkPHP’s method of using smarty template engine
  • 6 tips for php smarty template engine
  • Detailed explanation of how to use smarty templates in PHP based on Yii framework
  • First experience of Smarty templates in php
  • How to use PHP functions in smarty templates
  • How to generate html documents from php Smarty template
  • Summary of caching usage of PHP template engine Smarty
  • Cache application in php smarty template engine

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1104332.htmlTechArticlePHP file caching smarty template application example analysis, smarty application example This article analyzes the PHP file caching smarty template application example. Share it with everyone for your reference, the details are as follows: 1. Use...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!