一、模板修飾符
我們希望註冊的變量值不作影響而輸出時有相應的格式化效果,可以使用模板修飾符。
{$變量|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>