Smarty是一個PHP的模板引擎哆料。更明確來說暂氯,它可以幫助開發(fā)者更好地 分離程序邏輯和頁面顯示
一疮方、安裝
- 先從官網(wǎng)下在Smarty的壓縮包感论,解壓后笛丙,把目錄下的libs文件夾拷貝到你的php項目的目錄下逐工。
比如我的項目目錄情況是這樣的:
- libs
- templates 該目錄是Smarty默認(rèn)的模板存放路徑
- templates/index.tpl templates目錄下的模板文件 類似于html那種
- templates_c 該目錄是Smarty默認(rèn)的模板和php編譯后的文件
- index.php
在index.php中寫入以下代碼:
require "./libs/Smarty.class.php";
$smarty = new Smarty; // 創(chuàng)建一個Smarty實例
$smarty -> assign('name', 'xiaoqiang'); // 將模板可能會用到的變量賦值到該實例化的對象中
$smarty -> display('index.tpl'); // 內(nèi)部主要靠調(diào)用this -> compiler這個函數(shù)進行編譯轉(zhuǎn)換展示某個模板
在index.tpl寫入以下代碼:
...
<body>
我的名字是{$name}
</body>
...
開啟php腳本后么抗,訪問本地index.php就能看到變量name被展示出來了.
二远荠、擴展設(shè)置
在index.php中寫入以下代碼:
require "./libs/Smarty.class.php";
$smarty = new Smarty; // 創(chuàng)建一個Smarty實例
// 下面是一些常用的配置的改變
$smarty - >template_dir = './templates/'; // 設(shè)置模板的存放位置
$smarty - >compile_dir = './templates_c/'; // 設(shè)置編譯后的文件存放的位置
$smarty - >config_dir = './configs/'; // 設(shè)置配置文件的存放位置
$smarty - >cache_dir = './cache/'; // 設(shè)置緩存文件的存放位置
$smarty - >left_delimiter = '{' // 設(shè)置左邊界符
$smarty - >right_delimiter = '}' // 設(shè)置右邊界符
$smarty -> assign('name', 'xiaoqiang'); // 將模板用到的變量賦值到該實例化的對象中
$smarty -> display('index.tpl'); // 展示某個模板
三赘那、基礎(chǔ)語法
1.注釋
{* 我是注釋的內(nèi)容 *}
2.從php中分配變量
index.php:
...
$smarty -> assign('name', 'xiaoqiang'); // 將模板用到的變量賦值到該實例化的對象中
$smarty->assign('Contacts',
array('fax' => '555-222-9876',
'email' => 'zaphod@slartibartfast.com',
'phone' => array('home' => '555-444-3333',
'cell' => '555-111-1234')));
...
index.tpl:
...
<body>
我的名字是{$name}
{$Contacts.fax}<br>
{$Contacts.email}<br>
{$Contacts.phone.home}<br>
{$Contacts.phone.cell}<br>
</body>
...
3.Smarty中保留的php變量
index.tpl:
...
<body>
<p>當(dāng)前用戶名: {$smarty.get.username|cat:" yesterday."}</p>
<p>當(dāng)前密碼: {$smarty.get.password}</p>
<p>當(dāng)前時間戳: {$smarty.now|date_format:"%Y/%m/%d"}</p>
<p>{$smarty.post.page}</p>
<p>{$smarty.cookies.username}</p>
<p>{$smarty.server.SERVER_NAME}</p>
<p>{$smarty.env.PATH}</p>
<p>{$smarty.session.id}</p>
<p>{$smarty.request.username}</p>
</body>
...
4.變量的過濾器刑桑、修飾器(類似于Vue的filter)
常用的修飾器有:
capitalize // 將變量里的所有單詞首字大寫。
count_characters // 計算變量里的字符數(shù)
cat // 將cat里的值連接到給定的變量后面.
date_format // 格式化從函數(shù)strftime()獲得的時間和日期(時間戳)漓概。
default 為空變量設(shè)置一個默認(rèn)值漾月。當(dāng)變量為空或者未分配的時候,將由給定的默認(rèn)值替代輸出。
escape // 用于html轉(zhuǎn)碼,url轉(zhuǎn)碼
lower胃珍、upper // 將值全部轉(zhuǎn)換成大寫或小寫
regex_replace // 尋找和替換正則表達式
replace // 尋找內(nèi)容并替換掉指定的內(nèi)容
string_format // 格式化字符串的方法.例如格式化為十進制數(shù)
truncate // 從字符串開始處截取某長度的字符.默認(rèn)是80個.
你也可以指定第二個參數(shù)作為追加在截取字符串后面的文本字串.該追加字串被計算在截取長度中梁肿。
默認(rèn)情況下,smarty會截取到一個詞的末尾。
如果你想要精確的截取多少個字符,把第三個參數(shù)改為"true"
具體參考Smarty的手冊觅彰,這里就不一一列舉了吩蔑。
另外,對于同一個變量填抬,你可以使用多個修改器烛芬。它們將從左到右按照設(shè)定好的順序被依次組合使用。使用時必須要用"|"字符作為它們之間的分隔符飒责。
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|upper|spacify|truncate:30:". . ."}
5.常用內(nèi)建函數(shù)
config_load // 用于加載配置文件
foreach, foreachelse // 用于對數(shù)組進行遍歷
include // 用于在當(dāng)前模板中包含其它模板
include_php // 用于引入php文件
insert // insert 函數(shù)類似欲 inluce 函數(shù)赘娄,不同之處是 insert 所包含的內(nèi)容不會被緩存,每次調(diào)用該模板都會重新執(zhí)行該函數(shù).
if,elseif,else // 條件判斷語句宏蛉,與php中的類似
ldelim,rdelim // 輸出 {或}這兩個字符遣臼,類似于 那種
literal // 標(biāo)記使得Smarty不解析該段區(qū)域代碼
section,sectionelse // 模板的 section 用于遍歷數(shù)組中的數(shù)據(jù). section 標(biāo)簽必須成對出現(xiàn). 必須設(shè)置 name 和 loop 屬性. 名稱可以是包含字母、數(shù)字和下劃線的任意組合. 可以嵌套但必須保證嵌套的 name 唯一. 變量 loop (通常是數(shù)組)決定循環(huán)執(zhí)行的次數(shù). 當(dāng)需要在 section 循環(huán)內(nèi)輸出變量時拾并,必須在變量后加上中括號包含著的 name 變量. sectionelse 當(dāng) loop 變量無值時被執(zhí)行.
index 獲取index值揍堰,比如獲取section循環(huán)中的index值, {$smarty.section.customer.index}
index_prev, index_next // 用戶獲取上一個或下一個循環(huán)的值
iteration // iteration 不像index屬性受start嗅义、step和max屬性的影響屏歹,該值總是從1開始(index是從0開始的).rownum 是iteration的別名,兩者等同.
first之碗、last // 返回Boolean值蝙眶,用于判斷某個值是不是循環(huán)中的第一個值或最后一個 {$smarty.section.customer.first} {$smarty.section.customer.last}
6.自定義函數(shù)
assign assign 用于在模板被執(zhí)行時為模板變量賦值. {assign var="name" value="Bob"}
counter 用于輸出一個記數(shù)過程. counter 保存了每次記數(shù)時的當(dāng)前記數(shù)值
html_checkboxes html中的checkbox {html_checkboxes name="id" options=$cust_checkboxes checked=$customer_id separator="<br />"}
html_image html中的img標(biāo)簽 {html_image file="/path/from/docroot/pumpkin.jpg"}
html_radios html中的radio標(biāo)簽 {html_radios values=$cust_ids checked=$customer_id output=$cust_names separator="<br />"}
html_table html中的table{html_table loop=$data cols=4 table_attr='border="0"'} {html_table loop=$data cols=4 tr_attr=$tr}
本篇總結(jié)參考了:
Smarty官方文檔