Smarty local Shutdown cache configuration _php Tutorial

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

First,Inser:

Defines a handler function to be used for a inser tag, 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.


Second,Register_block:

Defines a block:smarty_block_name ($params, $content, & $smarty) {return $content;}//name represents the domain name of the zone

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:

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

return $content;

}

?>

(2) Writing programs and templates

Example program: testcacheless.php

Include (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}

Not cached: {$smarty. Now}

{/cacheless}

Run it now and find that it's not working, and both lines are cached.

(3) Rewrite Smarty_Compiler.class.php (Note: This file is important, please back up first to restore if necessary)

Find $this->_plugins[block][$tag _command] = Array ($plugin _func, NULL, NULL, NULL, TRUE); //My Line in 705

Modified to:

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

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

You can also directly change the last parameter of the original sentence to false, I do not understand the internal mechanism of smarty, so add a judgment, as long as the block is cacheless not cache

(4) OK, now clear the Template_c in the compilation file, re-run, it worked?

  • Source: Programming House
  • http://www.bkjia.com/ phpjc/486228.html www.bkjia.com true http://www.bkjia.com/phpjc/ 486228.html techarticle

  • Related Article

    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.