【PHP高级】Smarty模板四


一、模板修饰符

我们希望注册的变量值不作影响而输出时有相应的格式化效果,可以使用模板修饰符。

{$变量|capitalize}    每个单词首字母大写

{$变量|strip_tags}    使HTML标签、JS代码失效

{$变量|date_format:”%Y-%m-%d %H:%M:%S”}    时间戳转化为日期

其他

 

 

二、缓存

整页缓存局部更新。

1、新建缓存目录

2、配置缓存目录

3、开启并指定失效时间,局部更新定义函数处理

比如后台代码:

$smarty->cache_dir="cache";//指定缓存生成文件的路径

$smarty->caching=true;//开启缓存

$smarty->cache_lifetime=5;//缓存时间为5

前台测试只有指定的失效时间到达才会更新

<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}>   <{*日期采用缓存5秒钟才会更新*}>

4、后台定义局部更新函数

//局部更新的时间

function insert_GetNowTime(){

return date("Y-m-d H:i:s",time()+8*3600);   //必须要有返回值

}

5、前台页面掉用局部更新

<{insert name="GetNowTime"}>

如果需要参数

前台:

带参数在页面局部可以更新的时间:<{insert name="GetNowTime" a="AAA" b="BBB"}>

PHP

//局部更新的时间,参数为数组

function insert_GetNowTime($arr){

return date("Y-m-d H:i:s",time()+8*3600).$arr["a"].$arr["b"];   //必须要有返回值

}

示列源码

后台页面:

<?php

//引入核心类库文件

include_once('libs/SmartyBC.class.php');

$smarty=new SmartyBC();

//定义配置

//用户访问的是后台页面,所以所有的路径都应该要以后台页面为参照物!!

$smarty->setTemplateDir('templates');//定义模板路径

$smarty->setCompileDir('template_c');//定义编译路径

$smarty->setConfigDir('config');//定义配置路径

$smarty->setCacheDir('cache');//定义缓存路径

$smarty->setLeftDelimiter('<{');//指定左定界符,避免和JS冲突

$smarty->setRightDelimiter('}>');

 

$main2="这里是main2页面的内容";

$testa="<a href='#'>测试超链接</a>";

$testjs="<script>alert('aaa')</script>";

$date="time()";

$date1="time()+8*3600";

$smarty->assign("main",$main2);

$smarty->assign("testa",$testa);

$smarty->assign("testjs",$testjs);

$smarty->assign("date",$date);

$smarty->assign("date1",$date1);

//开启调试 :    $smarty->debugging=true;

//自动整合前后台页面

$smarty->display('main2.tpl');

 

?>

前台页面:

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

</head>

<body>

我是main2页面:<{$main|capitalize}><br>

超链接测试:<{$testa}><br>

JS代码测试:<{$testj}><br>

输出时间:<{$date|date_format:"%Y-%m-%d %H:%M:%S"}><br>

输出时间1<{$date1|date_format:"%Y-%m-%d %H:%M:%S"}><br>

输出系统当前时间:<{($smarty.now+8*3600)|date_format:"%Y-%m-%d %H:%M:%S"}><br>

</body>

</html>

 

 

发布了83 篇原创文章 · 获赞 12 · 访问量 13万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章