標籤:caching limit php檔案 repeat style 資料 關閉 基本使用 oca
一、
smarty
的特點
速度:相對於其他模板引擎,速度較快
編譯型:在下次訪問模板時直接存取編譯檔案,不再進行模板重新編譯
緩衝技術:可以將使用者最終看到的HTML檔案快取成一個靜態HTML
外掛程式技術:smarty可以自訂外掛程式,外掛程式其實是一些自訂函數
強大的表現邏輯:模板中可以使用if/else if/end if/foreach等
二、
smarty
的基本使用
(
1
)
index.php
頁面輸入如下
<?php
//1.引入smarty類
include ‘smarty/libs/Smarty.class.php‘;
//2.執行個體化smarty對象
$smarty = new Smarty();
//3.設定相關屬性
$smarty->template_dir = "templates"; //模板目錄
$smarty->compile_dir = "templates_c"; //編譯目錄
//修改定界符
$smarty->left_delimiter = ‘<{‘;
$smarty->right_delimiter = ‘}>‘;
//4.分配資料
$smarty->assign("title", "smarty模板引擎");
$smarty->assign("content", "smarty模板引擎的hello world");
//5.載入視圖
$smarty->display(‘index.html‘);
(
2
)在視圖頁面
index.html
輸入如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{$title}</title>
</head>
<body>
<h2>{$title}</h2>
<p>{$content}</p>
</body>
</html>
二、
smarty
的基本文法
(
1
)定界符
smarty中預設的定界符是{ }
① 任何在定界符之外的內容都是靜態,不會被解析(包括php代碼)
② 定界符開始的”{”符號和變數$之前不能有空格,例如{ $title}不會被解析
③ 頁面中的css或者js的{ }也會被認為是定界符,發生衝突,處理方式如下:
a ) 可以在{開頭的地方加上空格
b ) css和js以外部引入的方式添加
c ) 使用內建函數{literal} {需要解析的css或js} {/literal}
④ 修改定界符,在smarty配置的php頁面中輸入如下代碼:
$smarty->left_delimiter = ‘<{‘;
$smarty->right_delimiter = ‘}>‘;
(
2
)注釋
在index.html視圖頁面中使用<*注釋內容*>來書寫smarty的注釋
(
3
)變數
Smarty中的變數來源主要有以下三種:
① 通過PHP程式中的assign函數分配
② 保留變數
③ 組態變數
① assign
變數
php中的8種資料類型:
422陣容
4:四種標量類型;整型、浮點型、字串、布爾型
2:兩種複合類型;數組、對象
2:兩種特殊類型,資源和null
分配assign變數:
$user=array(“tom”,”jim”,”jack”);
$smarty->assign(‘user’,$user);
在視圖頁面中輸入{$user[0]},或者點文法{$user.0}可得到相應的值此處為tom
②
保留變數
無需在php中分配,直接可以在模板頁面中使用的變數。包括php中的超級全域變數,比如:$_GET,$_SERVER,以及smarty內建的一些變數
使用格式:{$smarty.保留變數名}
樣本:{$smarty.server.SERVER_NAME}
③
組態變數
無需在php程式中分配,但不同於保留變數,它是通過設定檔配置的
樣本如下:建立設定檔config.conf,輸入如下內容 ,設定檔必須建立在檔案夾名configs的檔案夾中
copyright="著作權資訊" //設定檔中的雙引號可以去除
police=備案資訊
[nationnality]
name=nationnality
[time]
name=time
php頁面調用smarty引擎
<?php
//1.引入smarty類
include ‘smarty/libs/Smarty.class.php‘;
//2.執行個體化smarty對象
$smarty = new Smarty();
//3.設定相關屬性
$smarty->template_dir = "templates";
$smarty->compile_dir = "templates_c";
$smarty->display(‘config.html‘);
調用配置資訊:
在視圖頁面中輸入如下內容即可調用配置資訊
{config_load file="test.conf" section="time"} //引用設定檔,並註明使用time這個部分
<h2>{#copyright#}</h2>
<h2>{$smarty.config.police}</h2>
<h2>{#name#}</h2>
//因為設定檔中有兩個name,負載檔案已指明為time部分,所以此處輸出time
(
4
)函數(
smarty
中函數主要分為三類)
①
內建函數
1. {if} {elseif} {else}
每一個{if}必須有配對的關閉標籤:{/if}
內建函數使用樣本:在視圖頁面中輸入如下
{if $iq >= 130}
Tom
{elseif $iq <130 && $iq>=110}
Jack
{elseif $iq<110 && $iq>=90}
Lucy
{else}
Jim
{/if} //配對的關閉標籤
2. {foreach}
使用foreach迴圈輸出二維數組樣本如下:
{foreach}的屬性,主要有下述6個:
a ) @index ,當前數組索引,從0開始計算
b ) @iteration,當前迴圈的次數,從1開始計算
c ) @first ,首次迴圈時,該值為true
d ) @last ,迴圈到最後一次時,該值為true
e ) @total ,總的迴圈次數,可在foreach內部使用,也可以在迴圈完成之後使用
f ) @show ,在foreach迴圈執行完之後,檢測迴圈是否顯示資料的判斷
使用方法樣本如下:以@first為例,給第一個tr添加類名
3. {section}
使用section迴圈時,注意,section不能用於關聯陣列,只能用於連續下標的數組(0,1,2,…),對關聯陣列使用迴圈,需要使用{foreach}
相關參數如下:
使用方式樣本:在視圖頁面中輸入如下
{sectionname=”item” start=0} 表示從第0項開始迴圈
<li>{$user[item]}</li> //此處的$user是一個索引數組
{/section}
使用index、first等屬性:
使用section可以在一次迴圈中遍曆多個數組:
②
變數修飾器
通常情況,在模板頁面中,直接輸出php程式中分配過來的變數即可,但也有一些特殊情況,需要對分配過來的變數/保留變數,進行再次處理,smarty提供了變數修飾器
使用樣本如下:
{$smarty.now|date_format:"%Y-%m-%d %T"} //使用了smarty的時間修飾器
{$content|truncate:10} //使用限制字元長度的修飾器
{"hello"|str_repeat:10} //重複輸出hello10次
……
③
函數外掛程式(自訂函數)
(1)html_radios
{html_radiosname=”names” values=$value output=$outlabs selected=”2”}
視圖頁面中調用這段代碼,相當於建立了對應的<input type=”radio”>提示資訊
此處的output就是input的外部的提示資訊
Selected=2表示第2個數值被選中
(2)html_checkbox //使用方法與html_radios基本相同
{html_checkboxname=”names” values=$value output=$outlabs selected=”Tom”}
如果要設定預設選中多個選項,以數組的形式設定selected的值即可
(3) html_options (下拉式清單)
{html_optionsname=”names” options=$array selected=3}
不需要設定在select下,option賦值一個關聯陣列即可,value就是數組的key
(4) cycle (交替迴圈值)
視圖頁面樣本: //輸出隔行添加的class
<tr class=”{cyclevalues=’one,two,three’}”>
<td>{$v.id}</td>
<td>{$v.name}</td>
<td>{$v.age}</td>
</tr>
三、
smarty
在項目中的引用
(
1
)引入
smarty
放在framework中,意味著smarty是架構的一部分
放在application中,是以第三方的方式引入smarty,(third_party)
(
2
)配置
smarty
此處以第三方引入方式為例:
在application>controllers>home>indexControler.class,php檔案中
publicfunction indexAction(){
//引入smarty類
IncludeAPP_PATH.”third_party/smarty/smarty.class.php”
//執行個體化smarty對象
$smarty= new Smarty();
//設定相關屬性
$smarty-> template_dir = CUR_VIEW_PATH . “templates”;
$smarty-> compile_dir = CUR_VIEW_PATH . “templates_c”;
//分配資料
$smarty-> assign(‘cats’,$cats);
$smarty-> assign(‘bestGoods’,$bestGoods);
//載入模板檔案
$smarty-> display(‘index.html’);
}
(
3
)配置
smarty
最佳化
(
BaseController.class.php
)
可以將上述(2)中的代碼寫到基礎控制類中,再讓其他控制器繼承自基礎控制價,這樣可以實現重複利用
(
4
)模板包含
可以將頭部html頁面提取出來,再引用include內建函數方法將提出的head.html頁面注入其他頁面中,{includefile = “head.html”}
四、
smarty
的緩衝
(
1
)緩衝的基礎使用
主要的緩衝方法分為:資料緩衝和檔案快取
smarty的緩衝屬於檔案快取:產生靜態頁面
smarty設定緩衝:
//開啟緩衝
$smarty->caching=true;
//設定緩衝目錄 (需要建立響應的檔案夾)
$smarty->cache_dir=CUR_VIEW_PATH.”cache”
//設定緩衝有效期間
$this->smarty->cache_lifetime=60; (預設有效期間為3600,單位秒)
//開啟smarty調用模式
$smarty->debugging=true; (可以開啟調試頁面)
//當前頁面是首頁
$smarty->assign(‘index’,true);
Smarty提供的判斷方法,判斷是否緩衝:isCached
使用樣本如下:
if(!$smarty->isCached(‘index.html’)){ //表明沒有緩衝
執行代碼
}
(
2
)局部不緩衝
①標籤的緩衝控制(nocache屬性)
顯示時間:{$smarty.now|date_format:’%Y-%m-%d%T’} 有緩衝重新整理時間不變
顯示時間:{$smarty.now|date_format:’%Y-%m-%d%T’ nocache}去除緩衝
②變數的緩衝控制(適用於單個變數,分配時第三個參數設為true)
聲明變數:$time=date(“Y-m-d H:i:s”);
$smarty->assign(“time1”,$time);
$smarty->assign(“time1”,$time,true); //聲明第三參數為true,該變數不緩衝
③模板地區的緩衝控制({nocache} {/nocache} 適用於一塊地區)
在視圖頁面使用{nocache}內建函數,去除緩衝,樣本如下:
{nocache}
<h3>{$smarty.now|date_format:’%Y-%m-%d%T’}</h3> //該內容不會緩衝
{/nocache}
(
3
)單模板多緩衝(同一個頁面產生多個快取檔案)
只需要在載入模板檔案時,輸入區分的參數即可(url中傳遞的參數)
$smarty->display(‘goods.html’,$goods_id);
同理判斷緩衝的時候也需要輸入這個參數:
$smarty->isCached(‘goods.html’,$goods_id)
(
4
)緩衝組
設定緩衝組:
$smarty->display(“list.html”,”$size|$brand|$style|$material”)
(
5
)刪除緩衝
緩衝失效情況:超過有效期間、模板頁面發生變化、刪除快取檔案
//刪除首頁緩衝
$smarty->clearCache(“index.html”);
//刪除頁面指定參數緩衝
$smarty->clearCache(“goods.html”,2);
//刪除所有緩衝
$smarty->clearAllCache();
刪除檔案使用的底層方法是unlink()函數
php模板引擎smarty