Smarty template engine caching mechanism detailed _php instance

Source: Internet
Author: User
Tags php code php template smarty template

This example describes the Smarty template engine caching mechanism. Share to everyone for your reference, specific as follows:

First of all, smarty caching and compiling, this is two different concepts, the compiler is started by default, and the caching mechanism needs to be opened, Smarty compiled files or php files, so the implementation of the time or compiled, if it involves the database, or to access the database so the cost is not small, So need to smarty cache to solve!

1. Turn on global caching

$smarty->cache_dir = "/caches/"; Cache directory
$smarty->caching = true;//open cache, invalid cache for flase
$smarty->cache_lifetime = 3600;//Cache time

2. One page uses multiple caches

For example: An article template page will generate multiple article pages, of course, is cached into many pages, the implementation is very simple, as long as the display () method set the second parameter, specify a unique identifier. The following PHP code:

$smarty->display (' Index.tpl ', $_get["article_id"]);

As above, cache an article page with the ID of the second argument article.

3. Reduce overhead for caching

In other words, the cached pages do not have to do the operation of the database processing, can be judged by is_cached () Method!

if (! $smarty->is_cached (' Index.tpl ')) {
 //Call Database
}
$smarty->display (' Index.tpl ');

4. Clear Cache

Generally in the development process is not open cache, because the output in the cache time is unchanged, but in the application process to open the cache can greatly improve the Web performance, clear the caching method is as follows:

Clear_all_cache ()//Clear All cache
Clear_cache (' index.tpl ');/purge Index.tpl cache
clear_cache (' Index.tpl ', cache_id) ;//Clear cache of specified IDs

5. Close 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 name of the user login to turn off the cache, Smarty provides the following three solutions:

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

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

Parameter passed through $params

can also be made into insert plug-ins, filename named: insert.xx.php, Function named: Smarty_insert_aa ($params,& $smarty), XX definition ditto

(2) $smarty->register_block ($params, & $smarty) so that a piece of the entire page is not cached

Define a block:

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

Register BLOCK:

$smarty->register_block (name, Smarty_block_name, false);
The third argument false indicates that the range is not cached

Template wording:

{Name} content {/name}

Write Block plugin:

The first step: Define a Plug-in function: block.cacheless.php, placed in the Smarty plugins directory

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

<?php
function smarty_block_cacheless ($param, $content, & $smarty) {return
$content
}
? >

Step two: Write Programs and templates

Sample program: testcacheless.php

<?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}<br>
{cacheless}
is not cached: {$smarty. Now}
{/cacheless}

Now run it and find it's not working, and both lines are cached.

Step three: rewrite Smarty_Compiler.class.php ( Note: This file is important, please back up to restore if necessary )

Find:

Copy Code code as follows:
$this->_plugins[block][$tag _command] = Array ($plugin _func, NULL, NULL, NULL, TRUE);

Modified into:

if ($tag _command = = cacheless) $this->_plugins[block][$tag _command] = Array ($plugin _func, NULL, NULL, NULL, FALSE); C1/>else $this->_plugins[block][$tag _command] = Array ($plugin _func, NULL, NULL, NULL, TRUE);

You can also directly change the last argument of the original sentence to false, that is, to turn off the default cache.

(3) using Register_function to prevent Plug-ins from being exported 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 ');

Annotations:

Define a function with a function name format:smarty_type_name ($params, & $smarty)
type is a function

Name is the user's custom label name, here is {current_time}

Two parameters are required, even if they are not used in the function. Two parameters are functionally ibid.

More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on Smarty template.

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.