Table des matières
Smarty高级应用之缓存操作技巧分析,smarty缓存操作技巧
Maison développement back-end tutoriel php Smarty高级应用之缓存操作技巧分析,smarty缓存操作技巧_PHP教程

Smarty高级应用之缓存操作技巧分析,smarty缓存操作技巧_PHP教程

Jul 12, 2016 am 08:51 AM
smarty 模板引擎 缓存

Smarty高级应用之缓存操作技巧分析,smarty缓存操作技巧

本文实例讲述了Smarty高级应用之缓存操作技巧。分享给大家供大家参考,具体如下:

smarty缓存控制

smarty提供了强大的缓存功能。但有时我们并不希望整篇文档都被缓存,而是有选择的缓存某一部分内容或某一部分内容不被缓存。例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定,同时需要一个函数取广告条的内容信息。smarty也提供了这种缓存控制能力。

我们可以使用{insert}使模板的一部分不被缓存

可以使用$smarty->register_function($params,&$smarty)阻止插件从缓存中输出,

还可以使用$smarty->register_block($params,&$smarty)使整篇页面中的某一块不被缓存。

下面我们真对一个简单需求,分别说明这三种控制缓存输出的方法。

需求:被缓存的文档中当前时间不被缓存,随每次刷新而变化。

1、使用insert函数使模板的一部分不被缓存

index.tpl:

<div>{insert name="get_current_time"}</div>

Copier après la connexion

index.php

function insert_get_current_time(){
  return date("Y-m-d H:m:s");
}
$smarty=new smarty();
$smarty->caching = true;
if(!$smarty->is_cached()){
  .......
}
$smarty->display('index.tpl');

Copier après la connexion

注解:

定义一个函数,函数名格式为:

inser_name(array $params, object &$smarty),

Copier après la connexion

函数参数可选的,如果在模板的insert方法中需要加入其他属性,就会作为数组传递给用户定义的函数。

如:

{insert name='get_current_time' local='zh'}

Copier après la connexion

在get_current_time函数中我们就可以通过$params['local']来获得属性值。

如果在get_current_time函数中需要用到当前smarty对象的方法或属性,就可以通过第二个参数获得。

这时你会发现index.tpl已被缓存,但当前时间却随每次刷新在不断变化。

2、使用register_function阻止插件从缓存中输出

index.tpl:

<div>{current_time}{/div}

Copier après la connexion

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');

Copier après la connexion

注解:

定义一个函数,函数名格式为:smarty_type_name($params, &$smarty)
type为function

name为用户自定义标签名称,在这里是{current_time}

两个参数是必须的,即使在函数中没有使用也要写上。两个参数的功能同上。

3、使用register_block使整篇页面中的某一块不被缓存

index.tpl:

<div align='center'>
Page created: {"0"|date_format:"%D %H:%M:%S"}
{dynamic}
Now is: {"0"|date_format:"%D %H:%M:%S"}
... do other stuff ...
{/dynamic}
</div>

Copier après la connexion

index.php:

function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty = new Smarty;
$smarty->caching = true;
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
if(!$smarty->is_cached()){
  .......
}
$smarty->display('index.tpl');

Copier après la connexion

注解:

定义一个函数,函数名格式为:smarty_type_name($params, &$smarty)
type为block

name为用户自定义标签名称,在这里是{dynamic}

两个参数是必须的,即使在函数中没有使用也要写上。两个参数的功能同上。

4、总结

(1)对缓存的控制能力:

使用register_function和register_block能够方便的控制插件输出的缓冲能力,可以通过第三个参数控制是否缓存,默认是缓存的,需要我们显示设置为false,正如我们试验中的所做的那样复制代码 代码如下:$smarty->register_function('current_time','smarty_function_current_time',false);
但insert函数默认是不缓存的。并且这个属性不能修改。从这个意义上讲insert函数对缓存的控制能力似乎不如register_function和register_block强。

(2)使用方便性:

但是insert函数使用非常方便。不用显示注册,只要在当前请求过程中包含这个函数smarty就会自动在当前请求的过程中查找指定的函数。

当然register_function也可以做到不在运行时显示注册。但是那样做的效果跟其他模版函数一样,统统被缓存,并且不能控制。

如果你使用在运行时显示调用register_function注册自定义函数,那么一定要在调用is_cached()方法前完成函数的注册工作。

否则在is_cached()这一步缓存文档将因为找不到注册函数而导致smarty错误

Smarty用户自定义函数实例

<&#63;php
$smarty->register_function('date_now', 'print_current_date');
function print_current_date($params, &$smarty)
{
 if(empty($params['format'])) {
  $format = "%b %e, %Y";
 } else {
  $format = $params['format'];
 }
 return strftime($format,time());
}
&#63;>

Copier après la connexion

在模板中使用

{date_now}
{* or to format differently *}
{date_now format="%Y/%m/%d"}

Copier après la connexion

更多关于Smarty相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127907.htmlTechArticleSmarty高级应用之缓存操作技巧分析,smarty缓存操作技巧 本文实例讲述了Smarty高级应用之缓存操作技巧。分享给大家供大家参考,具体如下:...
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Où sont stockés les fichiers vidéo dans le cache du navigateur ? Où sont stockés les fichiers vidéo dans le cache du navigateur ? Feb 19, 2024 pm 05:09 PM

Où sont stockés les fichiers vidéo dans le cache du navigateur ?

Comment afficher et actualiser le cache DNS sous Linux Comment afficher et actualiser le cache DNS sous Linux Mar 07, 2024 am 08:43 AM

Comment afficher et actualiser le cache DNS sous Linux

Les fichiers HTML seront-ils mis en cache ? Les fichiers HTML seront-ils mis en cache ? Feb 19, 2024 pm 01:51 PM

Les fichiers HTML seront-ils mis en cache ?

Accélérez vos applications : un guide simple sur la mise en cache Guava Accélérez vos applications : un guide simple sur la mise en cache Guava Jan 31, 2024 pm 09:11 PM

Accélérez vos applications : un guide simple sur la mise en cache Guava

Meilleures pratiques APCu : améliorer l'efficacité de vos applications Meilleures pratiques APCu : améliorer l'efficacité de vos applications Mar 01, 2024 pm 10:58 PM

Meilleures pratiques APCu : améliorer l'efficacité de vos applications

Une plongée approfondie dans la technologie de mise en cache PHP : la clé pour accélérer les performances d'un site Web Une plongée approfondie dans la technologie de mise en cache PHP : la clé pour accélérer les performances d'un site Web Jan 23, 2024 am 08:37 AM

Une plongée approfondie dans la technologie de mise en cache PHP : la clé pour accélérer les performances d'un site Web

Comment enregistrer des fichiers vidéo du cache du navigateur vers le local Comment enregistrer des fichiers vidéo du cache du navigateur vers le local Feb 23, 2024 pm 06:45 PM

Comment enregistrer des fichiers vidéo du cache du navigateur vers le local

Mécanisme de mise en cache et pratique d'application dans le développement PHP Mécanisme de mise en cache et pratique d'application dans le développement PHP May 09, 2024 pm 01:30 PM

Mécanisme de mise en cache et pratique d'application dans le développement PHP

See all articles