php模板引擎smarty的變量操做符可用於操做變量,自定義函數和字符。
語法中使用"|"應用變量操做符,多個參數用":"??指簟?/DIV>
capitalize[首字母大寫]
count_characters[計算字符數]
cat[鏈接字符串]
count_paragraphs[計算段落數]
count_sentences[計算句數]
count_words[計算詞數]
date_format[時間格式]
default[默認]
escape[轉碼]
indent[縮進]
lower[小寫 ]
nl2br[換行符替換成<br />]
regex_replace[正則替換]
replace[替換]
spacify[插空]
string_format[字符串格式化]
strip[去除(多餘空格)]
strip_tags[去除html標籤]
truncate[截取]
upper[大寫]
wordwrap[行寬約束]
組合使用多個操做符
實例:
{* 標題大寫 *}
<h2>{$title|upper}</h2>javascript
{* 取其前40個字符 *}
Topic: {$topic|truncate:40:"..."}php
{* 格式化文字串 *}
{"now"|date_format:"%Y/%m/%d"}html
{* 在自定義函數裏應用調節器 *}
{mailto|upper address="main@cn-web.com"}
capitalize(首字母大寫)
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|capitalize}
OUTPUT:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.java
count_characters(計算變量裏的字符數)mysql
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|count_characters}
OUTPUT:
Cold Wave Linked to Temperatures.
32
cat(鏈接字符串)
將cat裏的值鏈接到給定的變量後面
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle|cat:" yesterday."}
OUTPUT:
Psychics predict world didn't end yesterday.web
count_paragraphs(計算段數)
計算變量裏的段落數量
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|count_paragraphs}
OUTPUT:
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2正則表達式
count_sentences(計算句數)
計算變量裏句子的數量
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|count_sentences}
OUTPUT:
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2
count_words(計算詞數)
計算變量裏的詞數
index.php:sql
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|count_words}
OUTPUT:
Dealers Will Hear Car Talk at Noon.
7
date_format(日期格式)
Parameter Position
參數位置 Type Required Default Description
1 string No %b %e, %Y This is the format for the outputted date.
輸出字串的格式
2 string No n/a This is the default date if the input is empty.
輸入爲空時的默認設置express
在給定的函數serftime();裏格式日期和時間.
Unix或者mysql等的時間戳(parsable by strtotime)均可以傳遞到smarty.
設計者能夠使用date_format徹底控制日期格式.
若是傳給date_format的數據是空的,將使用第二個參數做爲時間格式
index.php:
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
index.tpl:
{$smarty.now|date_format}
{$smarty.now|date_format:"%A, %B %e, %Y"}
{$smarty.now|date_format:"%H:%M:%S"}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:"%H:%M:%S"}
OUTPUT:
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00api
default(默認)
Parameter Position Type Required Default Description
1 string No empty This is the default value to output if the variable is empty.
這是變量爲空的時候的默認輸出
爲空變量設置一個默認值.
當變量爲空或者未分配的時候,將由給定的默認值替代輸出.
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}
OUTPUT:
Dealers Will Hear Car Talk at Noon.
no title
escape(轉碼)
Parameter Position Type Required Possible Values Default Description
1 string No html,htmlall,url,quotes,hex,hexentity,javascript html This is the escape format to use.
用於html轉碼,url轉碼,在沒有轉碼的變量上轉換單引號,十六進制轉碼,十六進制美化,或者javascript轉碼.
默認是html轉碼
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a
href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
OUTPUT:
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan'
'Stiff Opposition Expected to Casketless Funeral Plan'
<a
href="bob@me.netmailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net</a>
indent(縮進)
Parameter Position Type Required Default Description
1 integer No 4 This determines how many characters to indent to.
2 string No (one space) This is the character used to indent with.
在每行縮進字符串,默認是4個字符(pear標準也是).
做爲可選參數,你能夠指定縮進字符數.
做爲第二個可選參數,你能夠指定縮進用什麼字符代替
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"t"}
OUTPUT:
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
lower(小寫)
將變量字符串小寫
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|lower}
OUTPUT:
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.
nl2br(換行符替換成<br /> )
全部的換行符將被替換成 <br />.同php的nl2br()函數同樣.
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Sun or rain expectedntoday, dark tonight");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle|nl2br}
OUTPUT:
Sun or rain expected<br />today, dark tonight
regex_replace(正則替換)
尋找和替換正則表達式 .
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
替換正則表達式.
2 string Yes n/a This is the string of text to replace with.
使用什麼文本字串來替換
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');
index.tpl:
{* replace each carriage return, tab & new line with a space *}{* 使用空格替換每一個回車,tab,和換行符 *}
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}
OUTPUT:
Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.
replace(替換)
簡單的搜索和替換字符串
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
將被替換的字符串
2 string Yes n/a This is the string of text to replace with.
用來替換的文本
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT:
Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
spacify
是一種在字符串的每一個字符之間插入空格或者插入其餘的字符(串).
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT:
Something Went Wrong in Jet Crash, Experts Say.
S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y .
S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^.
string_format(字符串格式化)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
使用的格式化方式
是一種格式化浮點數的方法.例如十進制數.使用sprintf語法格式化
index.php:
$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
index.tpl:
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT:
23.5787446
23.58
24
strip(去除(多餘空格)
替換全部重複的空格,換行和tab爲單個.
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}
OUTPUT:
Grandmother of
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.
strip_tags(去除html標籤)
去除在<和>之間的全部標籤,包括<和>.
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|strip_tags}
OUTPUT:
Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.
truncate(截取)
Parameter Position Type Required Default Description
1 integer No 80 This determines how many characters to truncate to.
指定截取多少字符
2 string No ... This is the text to append if truncation occurs.
截取後加在截取詞後的字符串
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).
檢查是否截取到詞的邊界
截取字符串開始的一段.默認是80個.
你能夠指定第二個參數做爲在截取的那段字符串後加上什麼字符.
默認狀況下,smarty會截取到一個詞的末尾,
若是你想要精確的截取多少個字符,把第三個參數改成"true"
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT:
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
upper(大寫 )
將變量改成大寫
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|upper}
OUTPUT:
If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
wordwrap(行寬約束)
能夠指定段落的寬度(也就是多少個字符一行,超過這個字符數換行).默認80.
第二個參數可選,能夠指定在約束點使用什麼字符(默認是換行符n).
默認狀況下smarty將截取到詞尾,你也能夠指定精確截取多少個字符
Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap to.
指 定段落(句子)的寬度
2 string No n This is the string used to wrap words with.
使用什麼字符約束
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (true).
是否精確約束到字符
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"<br>n"}
{$articleTitle|wordwrap:30:"n":true}
OUTPUT:
Blind woman gets new kidney from dad she hasn't seen in years.
Blind woman gets new kidney
from dad she hasn't seen in
years.
Blind woman gets new
kidney from dad she
hasn't seen in
years.
Blind woman gets new kidney<br>
from dad she hasn't seen in years.
Blind woman gets new kidney fr
om dad she hasn't seen in year
s.
組合使用多個操做符能夠在一個變量上應用操做符,它們將從左到右依次組合應用.多個操做符必須用"|"符號分開. index.php:$smarty = new Smarty;$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|upper|spacify}{$articleTitle|lower|spacify|truncate}{$articleTitle|lower|truncate:30|spacify}{$articleTitle|lower|spacify|truncate:30:". .}