smarty变量调节器和smarty缓存

smarty变量调节器
indent:缩进,indent(参数1:决定缩进多少个字符,默认4个;参数2:用什么字符来代替缩进,空格使用 )缩进,每一段左缩进。
replace:替换,replace(参数1:将被替换的文本字符串;参数2;用来替换的文本字符)
lower:将变量字符串改成小写
upper:将变量改成大写
truncate:截取,truncate(参数1:截取字符的数量;参数2:截取后追加在截取词后面的字符串;参数3:是截取到词的边界(假)还是精确到字符(真)默认是假,截取到边界)
spacify:插空。是一种在字符串中的每个字符之间插入空格或其他的字符(串),默认是插入空格

1、bltiaojieqi.php
<?php
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="../demo/templates";
$smarty->compile_dir="../demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="../demo/config";//配置文件存放路径


$string="ni hoa\n php!java,net.3g";//\n浏览器不识别,在后台(php)能读出是换行,页面显示不出来
$smarty->assign("cap",$string);
$smarty->display("bltiaojieqi.tpl");

?>

2、bltiaojieqi.tpl
<{$cap}>应用缩进indent之后:<{$cap|indent:4:"&nbsp;"}> <{*每一段左缩进*}> <br>
<hr>
<{$cap}>转化小写lower<{$cap|lower}><br>
<{$cap}>大写upper<{$cap|upper}>
<hr>
<{$cap}>用replace里德php天成java<{$cap|replace:"php":"java"}>
<hr>
<{$cap}>应用truncate截取字符串1<{$cap|truncate:12}> <{*截取12个字符;是截取到词的边界(假)还是精确到字符(真),默认假,本身12个字符截取到h,但假,截取到边界,而php整个单词并没有截完,所以就截取到php前面的边界*}> <br>

<{$cap}>应用truncate截取字符串2<{$cap|truncate:12:"**":true}> <{*精确到摸个字符*}><br>
<{$cap}>应用truncate截取字符串3<{*$cap|truncate:8:12:"**":true*}><{*从第8个字符开始截;modifier.truncate.php中默认的是0,更改写个参数start,开始截取的位置,默认也是0*}>
<hr>
<{$cap}>应用是spacify1:<{$cap|spacify}>
<br><{*是一种在字符串的每个字符之间插入空格或者其他的字符(串).
。默认是插入空格*}>
<{$cap}>应用是spacify2:<{$cap|spacify:"*"}><br>
<hr>
<{$cap}>应用是spacify:<{$cap|upper|spacify}><{*可以插入多个变量调节器;先执行upper换成大写,在执行spacify插入空格*}>

运行后的页面:

二、自定义变量调节器
1、把自定义的变量调节器文件放在lib/plugins/的下面。
1、modifier.changecolor.php  //定义变量调节器文件
<?php

function smarty_modifier_changecolor($string,$color='red'){
 $string="<font color='".$color."'>".$string."</font>";
return $string;
 }


?>

2、bltiaojieqi.php
<?php
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="../demo/templates";
$smarty->compile_dir="../demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="../demo/config";//配置文件存放路径


$string="ni hoa\n php!java,net.3g";//\n浏览器不识别,在后台(php)能读出是换行,页面显示不出来
$smarty->assign("cap",$string);
$smarty->display("bltiaojieqi.tpl");

?>
3、bltiaojieqi.tpl
<body>
<{$cap}>应用自定义修改器<{$cap|changecolor}><br>
<{$cap}>应用自定义修改器<{$cap|changecolor:green}><{*changecolor要与libs/plugins/modifier.changecolor.php的名称changecolor,还有modifier.changecolor.php里的函数名smarty_modifier_changecolor保持一致。相当于定义了一个changecolor调节器,就是一个特殊的词*}>
</body>

运行后的页面:

三、smarty缓存
1.什么是缓存(cache)?
内存(临时存放数据,解决cpu和外部设备之间速度不匹配)
高速缓存(cache)(解决cpu和内存之间速度不匹配的问题)
作用:提高计算机数据的访问速度
网站设计中(软件开发中)
缓存的概念:在PHP中,缓存就是就是硬盘中的一块区域,将生成的不变的页面放入这块区域中,相当于直接放入了缓存。当用户重复访问相同页面的时候,直接从缓存区域调用页面。
2.如何应用缓存(smarty中)
(1)单页面单缓存(建立缓存)
步骤1:开启缓存($smarty->caching=true|$smarty->caching=2设置lifetime)
步骤2:设置缓存目录($smarty->cache_dir='')
步骤3:设置缓存文件的生存时间($smarty->cache_lifetime)
步骤4:利用display()或fetch()生成缓存文件

例:
1、huancun.php
<?php
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="../demo/templates";
$smarty->compile_dir="../demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="../demo/config";//配置文件存放路径
$smarty->caching=true;//开启缓存
$smarty->cache_dir="../demo/cache";//缓存文件存放的路径
$smarty->cache_lifetime=20;//设置缓存时间,20是秒


$string="ni hoa\n php";//\n浏览器不识别,在后台(php)能读出是换行,页面显示不出来
$smarty->assign("cap",$string);
$smarty->display("a.tpl");

?>

2、a.tpl

<body>
Capitalize:将变量里的所有单词首字大写<br>
前:<{$cap}><br>
后:<{$cap|capitalize}><br>
<hr>
count_characters:决定是否计算空格字符<br>
<{$cap}>的字符个数为(不计空格):<{$cap|count_characters}><br>
<{$cap}>的字符个数为(计空格):<{$cap|count_characters:true}><br>
<hr>
cat:连接字符串<br>
给字符串<{$cap}>连接***之后:<{$cap|cat:"***"}>

</body>

3、得到的缓存文件

138
a:4:{s:8:"template";a:1:{s:16:"bltiaojieqi1.tpl";b:1;}s:9:"timestamp";i:1352882311;s:7:"expires";i:1352882331;s:13:"cache_serials";a:0:{}}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
Capitalize:将变量里的所有单词首字大写<br>
前:ni hoa
 php<br>
后:Ni Hoa
 Php<br>
<hr>
count_characters:决定是否计算空格字符<br>
ni hoa
 php的字符个数为(不计空格):8<br>
ni hoa
 php的字符个数为(计空格):11<br>
<hr>
cat:连接字符串<br>
给字符串ni hoa
 php连接***之后:ni hoa
 php***

</body>
</html>

 

 这个就会在demo/cache文件下生成一个缓存文件,当即刷新页面时,直接从缓存区调出该页面,如果你的代码更改,浏览的页面也不会改变,因为你是直接从缓存区域调用页面。所以不会改变。你只能删除缓存文件,浏览的页面才会根据你更改的代码变化,或者设置$smarty->cache_lifetime=20;//设置缓存时间,20是秒。在20秒之后他会自动覆盖你以前的缓存文件,使新更改的内容覆盖以前的缓存文件,在访问时,就会调用覆盖好的缓存文件,浏览的页面也随之改变

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值