今天就跟大家聊聊有關(guān)Smarty中的變量操作符有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到運(yùn)城網(wǎng)站設(shè)計(jì)與運(yùn)城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋運(yùn)城地區(qū)。php模板引擎smarty的變量操作符可用于操作變量,自定義函數(shù)和字符。
語(yǔ)法中使用"|"應(yīng)用變量操作符,多個(gè)參數(shù)用":"??指簟?/DIV>
capitalize[首字母大寫]
count_characters[計(jì)算字符數(shù)]
cat[連接字符串]
count_paragraphs[計(jì)算段落數(shù)]
count_sentences[計(jì)算句數(shù)]
count_words[計(jì)算詞數(shù)]
date_format[時(shí)間格式]
default[默認(rèn)]
escape[轉(zhuǎn)碼]
indent[縮進(jìn)]
lower[小寫 ]
nl2br[換行符替換成<br />]
regex_replace[正則替換]
replace[替換]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html標(biāo)簽]
truncate[截取]
upper[大寫]
wordwrap[行寬約束]
組合使用多個(gè)操作符
實(shí)例如下:
復(fù)制代碼 代碼如下:
{* 標(biāo)題大寫 *}
<h3>{$title|upper}</h3>
{* 取其前40個(gè)字符 *}
Topic: {$topic|truncate:40:"..."}
{* 格式化文字串 *}
{"now"|date_format:"%Y/%m/%d"}
{* 在自定義函數(shù)里應(yīng)用調(diào)節(jié)器 *}
{mailto|upper address="main@cn-web.com"}
capitalize(首字母大寫)
index.php頁(yè)面如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
index.tpl頁(yè)面如下:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|capitalize}
OUTPUT輸出如下:
復(fù)制代碼 代碼如下:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.
count_characters(計(jì)算變量里的字符數(shù))
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
index.tpl頁(yè)面如下:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|count_characters}
OUTPUT輸出如下:
Cold Wave Linked to Temperatures.
cat(連接字符串)
將cat里的值連接到給定的變量后面
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');
index.tpl頁(yè)面如下:
復(fù)制代碼 代碼如下:
{$articleTitle|cat:" yesterday."}
OUTPUT輸出如下:
復(fù)制代碼 代碼如下:
Psychics predict world didn't end yesterday.
count_paragraphs(計(jì)算段數(shù))
計(jì)算變量里的段落數(shù)量
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');
$smarty->display('index.tpl');
index.tpl模板頁(yè)面如下:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|count_paragraphs}
OUTPUT輸出如下:
復(fù)制代碼 代碼如下:
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(計(jì)算句數(shù))
計(jì)算變量里句子的數(shù)量
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
index.tpl模板如下:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|count_sentences}
OUTPUT輸出如下:
復(fù)制代碼 代碼如下:
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2
count_words(計(jì)算詞數(shù))
計(jì)算變量里的詞數(shù)
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl模板如下:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|count_words}
OUTPUT輸出如下:
復(fù)制代碼 代碼如下:
Dealers Will Hear Car Talk at Noon.
7
date_format(日期格式)
Parameter Position
參數(shù)位置 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.
輸入為空時(shí)的默認(rèn)設(shè)置
在給定的函數(shù)serftime();里格式日期和時(shí)間.
Unix或者mysql等的時(shí)間戳(parsable by strtotime)都可以傳遞到smarty.
設(shè)計(jì)者可以使用date_format完全控制日期格式.
如果傳給date_format的數(shù)據(jù)是空的,將使用第二個(gè)參數(shù)作為時(shí)間格式
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
index.tpl:
復(fù)制代碼 代碼如下:
{$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輸出如下:
復(fù)制代碼 代碼如下:
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
default(默認(rèn))
Parameter Position Type Required Default Description
1 string No empty This is the default value to output if the variable is empty.
這是變量為空的時(shí)候的默認(rèn)輸出
為空變量設(shè)置一個(gè)默認(rèn)值.
當(dāng)變量為空或者未分配的時(shí)候,將由給定的默認(rèn)值替代輸出.
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
Dealers Will Hear Car Talk at Noon.
no title
escape(轉(zhuǎn)碼)
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轉(zhuǎn)碼,url轉(zhuǎn)碼,在沒(méi)有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號(hào),十六進(jìn)制轉(zhuǎn)碼,十六進(jìn)制美化,或者javascript轉(zhuǎn)碼.
默認(rèn)是html轉(zhuǎn)碼
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$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輸出:
復(fù)制代碼 代碼如下:
'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(縮進(jìn))
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.
在每行縮進(jìn)字符串,默認(rèn)是4個(gè)字符(pear標(biāo)準(zhǔn)也是).
作為可選參數(shù),你可以指定縮進(jìn)字符數(shù).
作為第二個(gè)可選參數(shù),你可以指定縮進(jìn)用什么字符代替
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"t"}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
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如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|lower}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.
nl2br(換行符替換成<br /> )
所有的換行符將被替換成 <br />.同php的nl2br()函數(shù)一樣.
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Sun or rain expectedntoday, dark tonight");
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle|nl2br}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
Sun or rain expected<br />today, dark tonight
regex_replace(正則替換)
尋找和替換正則表達(dá)式 .
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
替換正則表達(dá)式.
2 string Yes n/a This is the string of text to replace with.
使用什么文本字串來(lái)替換
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{* replace each carriage return, tab & new line with a space *}{* 使用空格替換每個(gè)回車,tab,和換行符 *}
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.
replace(替換)
簡(jiǎn)單的搜索和替換字符串
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.
用來(lái)替換的文本
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
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
是一種在字符串的每個(gè)字符之間插入空格或者插入其他的字符(串).
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
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)
使用的格式化方式
是一種格式化浮點(diǎn)數(shù)的方法.例如十進(jìn)制數(shù).使用sprintf語(yǔ)法格式化
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
23.5787446
23.58
24
strip(去除(多余空格)
替換所有重復(fù)的空格,換行和tab為單個(gè).
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
Grandmother of
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.
strip_tags(去除html標(biāo)簽)
去除在<和>之間的所有標(biāo)簽,包括<和>.
index.php如下:
復(fù)制代碼 代碼如下:
$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模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|strip_tags}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
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).
檢查是否截取到詞的邊界
截取字符串開始的一段.默認(rèn)是80個(gè).
你可以指定第二個(gè)參數(shù)作為在截取的那段字符串后加上什么字符.
默認(rèn)情況下,smarty會(huì)截取到一個(gè)詞的末尾,
如果你想要精確的截取多少個(gè)字符,把第三個(gè)參數(shù)改為"true"
index.php如下:
復(fù)制代碼 代碼如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
index.tpl模板:
復(fù)制代碼 代碼如下:
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT輸出:
復(fù)制代碼 代碼如下:
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(行寬約束)
可以指定段落的寬度(也就是多少個(gè)字符一行,超過(guò)這個(gè)字符數(shù)換行).默認(rèn)80.
第二個(gè)參數(shù)可選,可以指定在約束點(diǎn)使用什么字符(默認(rèn)是換行符n).
默認(rèn)情況下smarty將截取到詞尾,你也可以指定精確截取多少個(gè)字符
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 kidneyfrom dad she hasn't seen inyears.Blind woman gets newkidney from dad shehasn't seen inyears.Blind woman gets new kidney<br>from dad she hasn't seen in years.Blind woman gets new kidney from dad she hasn't seen in years.
組合使用多個(gè)操作符
可以在一個(gè)變量上應(yīng)用操作符,它們將從左到右依次組合應(yīng)用.多個(gè)操作符必須用"|"符號(hào)分開.
index.php頁(yè)面如下:
$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:". . ."}
OUTPUT輸出:
Smokers are Productive, but Death Cuts Efficiency.
S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y .
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
s m o k e r s a r e p r o d u c t i v e , b u t . . .
s m o k e r s a r e p. . .
看完上述內(nèi)容,你們對(duì)Smarty中的變量操作符有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前名稱:Smarty中的變量操作符有哪些-創(chuàng)新互聯(lián)
文章鏈接:http://jinyejixie.com/article44/ghghe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、定制開發(fā)、Google、虛擬主機(jī)、網(wǎng)站維護(hù)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容