注釋
{* 注釋內容 *}
輸出
比如,現在php文件中有一個數組,并且用assign
方法賦值給了一個模板文件test.tpl
$arr=array('title'=>'smarty的學習', 'author'=>'小明')
$smarty->assign('arr', $arr);```
然后我們在模板中輸出:
{$arr.title} // 方法一
{$arr['title']} // 方法二```
變量調節器
變量調節器,寫在變量后面,用豎線|
隔開。
1 - 首字母大寫 capitalize
示例:{$articleTitle|capitalize}
php文件:
$smarty->assign('articletitle', 'i ate an apple');
$smarty->display('test.tpl');```
tpl文件:```{$articletitle|capitalize}```
輸出:```I Ate An Apple```
**2 - 字符串連接 cat**
示例:```{$articleTitle|cat:"yesterday."}```
php文件:
$smarty->assign('articletitle', 'i ate an apple ');
$smarty->assign('yesterday', 'yesterday');
$smarty->assign('point', '.');
$smarty->display('test.tpl');tpl文件:
{$articletitle|cat:"yesterday.":"point"}```
輸出:i ate an apple yesterday.
3 - 日期格式化 date_format
示例:{$yesterday|date_format}
{$yesterday|date_format:" :"}
php文件:
$smarty->assign('time', time()); //time()為php內置函數,用于獲取unix格式的時間
$smarty->display('test.tpl');```
tpl文件:```{$time|date_format}```
輸出:`Feb 07, 2017`
or
tpl文件:```{$time|date_format:"%B %e, %Y %H:%M:%S"}```
輸出:`Feb 07, 2017 08:05:08 //注意:時間不對是因為沒有設置時區,當前時間為格林威治時間`
**4 - 為未賦值或為空的變量指定默認值default**
示例:```{$articleTitle|default:"no title"}```
php文件:
$smarty->assign('articletitle', ' ');
$smarty->display('test.tpl');tpl文件:
{$articletitle|default:"no title"}```
輸出:no title
5 - 轉碼 escape
用于html轉碼,url轉碼,在沒有轉碼的變量上轉換單引號,十六進制轉碼,十六進制美化,或者js轉碼。more為html轉碼。
php文件:
$smarty->assign('url', 'www.chuying-he.com');
$smarty->display('test.tpl');```
tpl文件:```{$url|escape:"url"} //escape:后面跟著的是轉碼方式```
輸出:`www.chuying-he.com`
**6 - 小寫 lower 大寫 upper**
將變量字符串小 / 大寫
示例:
```{$articletitle|lower}```
```{$articletitle|upper}```
php文件:
$smarty->assign('articletitle', 'Happy New Year');
$smarty->display('test.tpl');```
tpl文件:
{$articletitle|lower}
<br/>
{$articletitle|upper}```
輸出:
happy new year
HAPPY NEW YEAR
**7 - 將所有換行符替換成<br/> nl2br**
將所有換行符替換成<br/>,與PHP中的nl2br()函數一樣
示例:```{$articletitle|nl2br}```
php文件:
$smarty->assign('articletitle', 'Happy
New
Year');
$smarty->display('test.tpl');```
tpl文件:
{$articletitle|nl2br}```
輸出:
Happy
New
Year
**8 - 其他函數**
可參見手冊,但原則上應該通過php或者CSS直接處理完畢,在賦值到Smarty中,盡量少用Smarty函數
##Smarty的條件判斷語句
Smarty中,```eq```表示```==```,```neq```表示```!=```,```gt```表示```>```,```lt```表示```<```,
{if isset($name) && $name == 'Blog'}
{* do something }
{elseif $name == $foo}
{ do something *}
{/if}
##Smarty的循環語句
**第一種循環 section**
```{section}```, ```{sectionelse}``` 是Smarty的內置函數,詳細文檔點擊[這里](http://www.smarty.net/docs/zh_CN/language.function.section.tpl)。
php文件:
$articlelist=array(
array(
"title" => "My first article",
"author" => "Alex",
"content" => "I'm a web developer."
),
array(
"title" => "Work Note",
"author" => "John Doe",
"content" => "Where is my home?"
)
)
$smarty->assign("articlelist", $articlelist);
$smarty->display("test.tpl");
tpl文件:
{section name=article loop=$articlelist}
{$articlelist[article].title}
{$articlelist[article].author}
{$articlelist[article].content}
<br />
{/section}
輸出:
My first article Alex I'm a web developer.
Work Note John Doe Where is my home?```
第二種循環 foreach
{foreach}
{foreachelse}
用于循環數組,語法上比{section}更加簡單清晰,并且可以使用非數字下標。
php文件:同上
tpl文件:
{foreach item=article from=$articlelist}
{$article.title}
{$article.author}
{$article.content}
<br/>
{foreachelse}
NOTHING IN ARRAY
{/foreach}
輸出:同上
注意:{foreachelse}后面的內容會在當前數組中沒有內容時顯示
Smarty的文件引用
在PHP中,有兩種引入方式:include
和require
。在Smarty中只有include
這一種。把別的模板引入進來。
語法:{include file='page_header.tpl' sitename="sugar"}
解釋:file為要導入的模板名,sitename
中的值會傳遞到page_header.tpl
里面去,要注意的是,sitename這個值 能且只能 在page_header.tpl
里面使用。假如page_header.tpl
中有同名變量,該變量值會被sitename的值替代
Smarty類和對象的賦值與使用
assign
除了能賦值一個變量 or 數組,還能把一個類的對象以變量的形式賦值到smarty的模板當中去。
php文件:
class My_Object{
function methl($params){
return $params[0].' is already '.$params[1];
}
}
$myobj=new My_Object;
$smarty->assign('myobj',$myobj); // 將對象$myobj傳入test.tpl模板的名為'myobj'的對象中
$smarty->display('test.tpl');
tpl文件:
{$myobj->meth1(array('cake','done'))} // 這里的$myobj是從php文件中傳入的對象,該對象可使用類中的methl方法
輸出:
cake is already done
Smarty函數的使用
盡管Smarty中有大量的變量調節器,也還是有用戶希望使用其他方法,比如:
1. 使用php內置函數
php文件:
$smarty->assign('time', time());
$smarty->display('test.tpl');
tpl文件:
// date()為php的內置函數
// 第一次嘗試:{$time|date("Y-m-d")};
// ---- 注意:該寫法被解釋成:date這個函數有兩個參數,第一個參數為$time,第二個參數為"Y-m-d" ----
// 而php的date方法應該是第一個參數為日期格式,第二個參數為unix時間戳,所以我們要變換順序如下:
{"Y-m-d"|date($time)};
輸出:
2017-02-08```
**2. 使用自定義函數,并使用 ```registerPlugin``` 注冊到smarty模板里面使用**

**注意**:```registerPlugin```的第一個參數除了function,還有modifier,block等,下面在smarty的插件制作中會詳細講到
php文件:
function test($params){
$p1=$params['p1'];
$p2=$params['p2'];
return 'the first para is '.p1.', the second para is '.p2;
}
// 第一個參數表示被注冊程序的類型(plugin/modifier/block),第二個參數表示注冊到smarty中后函數的名字,第三個參數表示要傳入的參數名。
$smarty->registerPlugin('function', 'f_test', 'test');
$smarty->display('test.tpl');
tpl文件:
{f_test p1='abc' p2='edf'}; // {函數名 第一個參數 第二個參數} 即,將兩個參數傳送到函數f_test中
輸出:
```the first para is abc, the second para is edf```
**3. 自定義插件**
在第一和第二種方法中,我們發現,在最終的模板調用函數的時候,方法不同,格式也不同,這是非常不友好的書寫方式,為了解決該問題,我們需要一樣新的東西:smarty插件。
*-------- 什么是插件 --------*
插件是遵循原來的系統主體接口的編寫規范,編寫出來的程序,它能夠調用主系統的數據和函數庫。他的好處是可執行力強,可重復利用率高,本身的增刪改不影響主系統的結構。
Smarty中也提供了這種插件機制,能夠在不改變smarty主程序的前提下,增加更多豐富的功能。 Smarty的本質其實是function函數,它的書寫方式完全遵照php的函數語法。
*-------- Smarty插件類型 --------*
- functions 函數插件
- modifiers 修飾插件
- block functions 區塊函數插件
*-------- 插件制作方法 --------*
1. 使用```registerPlugin```方法注冊寫好的自定義函數(即方法2)
2. 將寫好的插件放入Smarty解壓目錄的lib下的plugins目錄中
3. php內置函數,可以作為修飾插件(變量調節器插件)的形式在模板中使用
*-------- 在```wamp/www/smarty/smarty/plugins```目錄中,可以看到許多php文件 --------*
- 以```function```開頭類似```function.counter.php```為函數插件
- 以```modifier```開頭類似```modifier.capitalize.php```為修飾插件
- 以```block```開頭類似```block.textformat.php```為區塊函數插件
注意:命名規則為```插件類型.插件名.php```
*-------- functions 函數插件舉例 --------*
// 名為 function.test1.php 的函數插件
<?php
// 命名規則: smarty_function_插件名(要和文件中的插件名一樣)
function smarty_function_test1($params){
$width = $params['width'];
$height = $param['height'];
$area = $width * $height;
return $area;
}
?>```
// test.php文件
$smarty->display('area.tpl');
// area.tpl 模板文件
//smarty對該語句的處理是:調用test函數,然后將兩個參數放在一個數組里面傳遞到test函數中array('width'=>150, 'height'=>200)
{test1 width=150 height=200}
-------- modifier 函數插件舉例 --------
// 名為modifier.test2.php的modifier插件
<?php
// 命名規則: smarty_modifier_插件名(要和文件中的插件名一樣)
// 該modifier定義兩個參數,第一個為unic時間戳,第二個為時間形式
function smarty_modifier_test2($utime, $format){
return date($format, $utime);
}
?>```
// test.php文件
$smarty->assign('time',time());
$smarty->display('datetime.tpl');```
// datetime.tpl 模板文件
{$time|test2:'Y-m-d H:i:s'}```
*-------- block 函數插件舉例 --------*
// 名為block.test3.php的block插件
<?php
function smarty_block_test3($params, $content){
$replace=$params['replace'];
$maxnum=$params['maxnum'];
if($replace=='true'){
$content=str_replace(',', ',', $content); // 將中文的逗號替換成英文的逗號
$content=str_replace('。','.', $content); // 將中文的句號替換成英文的句號
}
$content=substr($content, 0, $maxnum);
return $content;
}
?>```
// test.php文件
$smarty->assign('str','Hello, my name is HanMeiMei。What is your name?');
$smarty->display('content.tpl');```
// content.tpl 模板文件
{test3 replace='true' maxnum=29}
{$str}
{/test3}```