Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

控制输出缓存

通常情况下,开�缓存�以让整个最终输出的页�被缓存下�。 然而,Smarty 3�供了几�在输出缓存中控制部分区域�进行缓存的方法。

一般说明

注�在�缓存的区域内使用的全部��,在页��次加载时都会从PHP里�新赋值。

模�区域缓存控制

模�内的一�区域�以很容易地通过 {nocache} 和{/nocache} 标签�设置�进行缓存。

Example 15.10. 使模�的一�区域�进行缓存



今天是:
{nocache}
{$smarty.now|date_format}
{/nocache}

   

上�的代�将在一个缓存的页�上输出当�日期时间。


标签的缓存控制

控制�个标签�进行缓存,�以为其加入"nocache"的属性。

Example 15.11. 让标签内容�进行缓存


今天是:
{$smarty.now|date_format nocache}

    

��的缓存控制

你�以通过assign()�控制��值�进行缓存。

说明

如果一个��被设置了�缓存,那么它在页��次加载时都会从PHP里�新赋值。

说明

���缓存的设置,会在编译�的代�中起效。所以如果你�新设置了�个���缓存,那么你需�删除已�存在的编译和缓存文件,使得模��以�新编译并起效。

Example 15.12. 控制���缓存


// 赋值$foo,并将其设置��缓存的��
$smarty->assign('foo',time(),true);


动�的时间值是: {$foo}

    

�件的缓存控制

你�以在注册一个�件时,�时控制�件的缓存。 registerPlugin() 的第三个�数是$cacheable,其默认是TRUE。

当你注册一个�件时,�时设置$cacheable=false, 那么�件会在�次页�显示的时候都被调用,�使页�整体已�被缓存了。 �件函数的这个行为和{insert}很相似。

说明

�件�缓存的设置,会在编译�的代�中起效。所以如果你�新设置了�个�件�缓存,那么你需�删除已�存在的编译和缓存文件,使得模��以�新编译并起效。

对比{insert}也�以默认让�件�进行缓存。 它们�以通过第四个�数$cache_attrs�定义进行缓存。 $cache_attrs定义了一个需�进行缓存的属性数组, 然�这些属性会被写入缓存,以供�件�次执行的时候直接读�。

Example 15.13. 控制�件的缓存


<?php
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

function remaining_seconds($params, $smarty) {
    $remain = $params['endtime'] - time();
    if($remain >= 0){
        return $remain . ' second(s)';
    }else{
        return 'done';
    }
}

$smarty->registerPlugin('function','remaining', 'remaining_seconds', false, array('endtime'));

if (!$smarty->isCached('index.tpl')) {
    // 从db获�$obj 并赋值...
    $smarty->assignByRef('obj', $obj);
}

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

   

模�index.tpl 内容是:


时间还有: {remaining endtime=$obj->endtime}

   

�使页�被缓存了,$obj的倒计时也还是会在�次页�刷新的时候改�。 endtime属性仅在第一次页�显示的时候,被从db里�读�出�并写入到模�,然�就一直是直接从缓存内读�了。


Example 15.14. 使模�内一部分区域�缓存


index.php:

<?php
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

function smarty_block_dynamic($param, $content, $smarty) {
    return $content;
}
$smarty->registerPlugin('block','dynamic', 'smarty_block_dynamic', false);

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

   

模�index.tpl的内容:


页�创建于: {'0'|date_format:'%D %H:%M:%S'}

{dynamic}

现在时间是: {'0'|date_format:'%D %H:%M:%S'}

... �些其他事情 ...

{/dynamic}

   

刷新页��你�以�现两个日期的��。一个是“动�的�,�一个是“��的�。 你�以�上�的代�一样把任何东西放{dynamic}...{/dynamic}标签内,�使其�缓存。

说明

上�的例�是展示了动�的区��件是如何工作的。 ��如何通过{nocache} 和 {/nocache}标签�进行模�区域缓存控制。

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 1 plus 17? (Are you human?)

Advertisement