smarty模板使用詳細教程

smarty模板在當前的php項目開發過程中運用非常廣泛,若能熟練掌握smarty模板的使用,那麼對於MVC模型會更深的體會與理解(不理解什麼是MVC模型的,可以到如何理解MVC模型這篇文章看看!)。
1.下載並配置smarty
下載smarty的最新版,解壓,拷貝其中的libs文件夾到項目中。下載地址:http://www.xpgod.com/soft/5937.html

2.在項目中創建templates、templates_c、cache、config四個文件夾
在項目中創建index.php,代碼如下
<?php
require_once("libs/smarty.class.php");
$smarty=new smarty();
$smarty->template_dir="templates";//指定模板文件的路徑
$smarty->compile_dir="templates_c";//指定編譯的文件路徑
$smarty->cache_dir="cache";//指定緩存文件路徑
$smarty->config_dir="config";//指定smarty配置文件路徑
$smarty->left_delimiter="<{";//指定左定界符,避免和JS衝突
$smarty->right_delimiter="}>";
$smarty->assign("name","天涯的海風");//註冊變量
$smarty->display("index.tpl");//顯示模板
?>
接下來,在templates文件夾下創建.tpl的模板文件,顯示變量值
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>歡迎您:<{$name}></title>
</head>

<body>
</body>
</html>

3.if條件控制
PHP頁面代碼:
$temp=1;
$smarty->assign("temp",$temp);
模板前臺:
<{if $temp gt 0}>
臨時變量>0
<{else}>
臨時變量<=0
<{/if}>
其中
eq 判斷是否相等
neq、ne 不相等
lt 小於
lte 小於等於
gt 大於
gte 大於等於
is div by 被某數整除
is even 爲偶數
is odd 爲奇數

4.循環
$arr=array(1,2,3);
$smarty->assign("arr",$arr);

<{foreach from=$arr item=v key=k}>
 鍵:<{$k}>|值:<{$v}>
<{foreachelse}>     <{*smarty註釋,數組爲空時可以顯示對應內容*}>
 數組爲空
<{/foreach}>

<{section name=k loop=$arr}>  <{*不支持引用數組*}>
 值:<{$arr[k]}>&nbsp;
 <{sectionelse}>
 數組爲空
<{/section}>

5.smarty配置文件
(1)在config文件夾下創建smarty.conf文件,內容如下
#全局變量
title="歡迎進入海風網站"

#節點變量
[section1]
title="下節頁面"

(2)在templates下創建header.tpl頁面
<{config_load file="smarty.conf"}>  <{*引入配置文件*}>
<html>
<head>
<title><{$smarty.config.title}>或者<{#title#}></title>
</head>

<body>
</body>
</html>
在項目文件下創建後臺代碼並顯示,即可看到全局變量配置的效果

(3)頁面包含,並顯示子節點
承接上面的例子,修改index.tpl爲以下內容
<{include file="header.tpl"}>    <{*包含頭部已經製作好的頁面*}>
<{config_load file="smarty.conf" section="section1"}>  <{*加載配置文件,指定訪問子節點*}>
顯示子節點值:<{#title#}> 或者 <{$smarty.config.title}>

最終在頁面頭部標題顯示爲全部配置文件中的 歡迎進入海風網站
而在頁面主體部分則只會顯示子節點中  下面的頁面節點

(4)模板修飾符
<{$name|capitalize}>   <{*每個單詞的首字母大寫*}>
$smarty->assign("b","<b>haifeng</b>");//正常顯示HTML標籤表過
<{$b|strip_tags}>     <{*消除HTML標籤效果*}>
<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}>   <{*日期格式化*}>

(5)緩存與更新
$smarty->caching=true;//開啓緩存
$smarty->cache_lifetime=5;//緩存時間爲5秒

在後臺頁面中創建函數,著名函數名
function insert_gettime(){
 return date("Y-m-d H:i:s",time()+8*3600);   //必須要有返回值
}
在前臺調用的過程中會發現以下2中方式的區別:
<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}>   <{*緩存日期格式*}>
<{insert name="gettime"}>                <{*不緩存日期格式調用*}>

(6)讀取結果集並顯示的例子
PHP代碼
$result=$mysql->execute("select * from something");
while($row=mysql_fetch_array($result)){
 $arr1[]=$row;
}
$smarty->assign("arr1",$arr1);
前臺讀取,.後面是字段名
<{foreach from=$arr1 item=v}>
 <{$v.Sid}>&nbsp;
<{/foreach}>

(7)smarty調試
$smarty->debugging=true;


(8)頁面切割與合成
<{include_php file="header.php"}>


(9)普通for循環

<{section name=loop loop=$count} >
id: <{$smarty.section.loop.index} >
<{/section}>

(10)自定義變量

<{assign var="site" value=$v.RightName}>

取值<{$site}>

以上只是smarty模板教程部分的冰山一角,想要瞭解到更深層的smarty模板使用教程,就必須不斷地積累,以便在項目中熟練運用,根據項目需求實現所需的功能!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章