本文實(shí)例講述了PHP模板引擎Smarty內(nèi)置變量調(diào)解器用法。分享給大家供大家參考,具體如下:
Smarty 中的變量調(diào)解器相當(dāng)于函數(shù),其調(diào)用方式為:通過 "|" 后面直接跟調(diào)解器函數(shù)名,如果有參數(shù),得加在 ":" 后面,多個(gè)參數(shù)的話,累加即可。
下面為您介紹 Smarty 中內(nèi)置的變量調(diào)解器:
1、capitalize
將變量里的所有單詞首字大寫。參數(shù)值 boolean 型決定帶數(shù)字的單詞,首字是否大寫。默認(rèn)不大寫
index.php
$tpl->assign('str', 'hello world wor2ld!!!');$tpl->display('index.html'); index.html(模板文件)
<{$str|capitalize}><{$str|capitalize:true}> 結(jié)果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!
2、count_characters
計(jì)算變量里的字符數(shù),該調(diào)解器默認(rèn)不計(jì)算空格(空格、制表符、回車…)只計(jì)算字符的個(gè)數(shù),并且能很好的支持中文字符計(jì)算;如果添加參數(shù) true ,則計(jì)算空格。
index.html
<{$str|count_characters}> // 不計(jì)算空格<{$str|count_characters:true}> // 計(jì)算空格 結(jié)果為:13、14
3、cat
連接字符串,將cat里的值連接到給定的變量后面。
<{$str|cat:' Happy new year.'}> 結(jié)果為:hello world!!! Happy new year.
4、count_paragraphs
計(jì)算段數(shù),計(jì)算變量里的段落數(shù)量,完美支持中文段落。
index.php
$str = <<assign('str', $str);$tpl->display('index.html'); index.html
<{$str|count_paragraphs}> 結(jié)果為:3
5、count_sentences
計(jì)算句數(shù),計(jì)算變量里句子的數(shù)量。注:只支持英文語句,不支持中文。
index.php
$str = <<assign('str', $str); index.html
<{$str|count_sentences}> 結(jié)果為:2
6、count_words
計(jì)算詞數(shù),計(jì)算變量里的詞數(shù)。
index.php
$str = <<assign('str', $str); index.html
<{$str|count_words}> 結(jié)果為:12
7、date_format
日期格式化,具體參數(shù)很多,這里只舉例中國式日期格式
index.php
$tpl->assign('date', time()); // 傳遞時(shí)間戳 index.html
<{$date|date_format:'%Y-%m-%d %H:%M:%S'}> 結(jié)果為:2012-01-26 14:37:22
8、default
默認(rèn),為空變量設(shè)置一個(gè)默認(rèn)值,當(dāng)變量為空或者未分配的時(shí)候,將由給定的默認(rèn)值替代輸出。
index.php
$tpl->assign('str', ''); // 賦值給空 index.html
<{$str|default:'默認(rèn)輸出...'}>、<{$string|default:'沒有定義,默認(rèn)輸出...'}> 結(jié)果為:默認(rèn)輸出...、沒有定義,默認(rèn)輸出...
9、escape
轉(zhuǎn)碼,用于 html 轉(zhuǎn)碼,url 轉(zhuǎn)碼,在沒有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號(hào),十六進(jìn)制轉(zhuǎn)碼,十六進(jìn)制美化,或者 javascript 轉(zhuǎn)碼,默認(rèn)是html轉(zhuǎn)碼
index.php
$html = <<Googlehtml;$js = << for (var i=0; i<100; i++) { window.alert(i); }js;$tpl->assign('html', $html); // html$tpl->assign('url', 'http://www.google.com.hk'); // url$tpl->assign('js', $js); // javascript index.html
HTML 轉(zhuǎn)碼:<{$html|escape:"html"}>URL 轉(zhuǎn)碼:<{$url|escape:"url"}>JS 轉(zhuǎn)碼:<{$js|escape:"javascript"}> 結(jié)果為:
HTML 轉(zhuǎn)碼:GoogleURL 轉(zhuǎn)碼:http%3A%2F%2Fwww.google.com.hkJS 轉(zhuǎn)碼:
10、indent
縮進(jìn),每行縮進(jìn)字符串,第一個(gè)參數(shù)指定縮進(jìn)多少個(gè)字符串,默認(rèn)是四個(gè)字符;第二個(gè)參數(shù),指定縮進(jìn)用什么字符代替。
11、lower
小寫,將變量字符串小寫。
使用方法:<{$str|lower}>
12、upper
大寫,將變量改為大寫。
使用方法:<{$str|upper}>
13、nl2br
換行符替換成
所有的換行符將被替換成 ,同php的nl2br()函數(shù)一樣。
14、regex_replace
正則替換,尋找和替換正則表達(dá)式,和 preg_replace() 的語法一樣。
index.php
$tpl->assign('str', 'http://www.google.com'); index.html
<{$str|regex_replace:'/go{2}gle/':'baidu'}> 結(jié)果為:http://www.baidu.com
15、replace
替換,簡(jiǎn)單的搜索和替換字符串。
16、spacify
插空,插空(不知道這個(gè)詞是什么意思,顧名思義了^^)是一種在字符串的每個(gè)字符之間插入空格或者其他的字符(串)。
index.php
$tpl->assign('str', 'hello world!!!'); index.html
<{$str|spacify:"^^"}> 結(jié)果為:h^^e^^l^^l^^o^^ ^^w^^o^^r^^l^^d^^!^^!^^!
17、string_format
字符串格式化,是一種格式化浮點(diǎn)數(shù)的方法,例如:十進(jìn)制數(shù).使用 sprintf 語法格式化。
index.php
$tpl->assign('num', 23.5787446); index.html
<{$num|string_format:"%.2f"}><{$num|string_format:"%d"}> 結(jié)果為:23.58、23
18、strip
替換所有重復(fù)的空格、換行、tab 為單個(gè)
index.php
$tpl->assign('str', "Grandmother of/neight makes/t hole in one."); index.html
<{$str|strip:" "}> 結(jié)果為:Grandmother of eight makes hole in one.
源代碼:
Grandmother of eight makes hole in one.
19、strip_tags
去除在<和>之間的所有標(biāo)簽,包括<和>。
index.php
$tpl->assign('str', "Google"); index.html
<{$str|strip_tags}> 結(jié)果為:Google(源代碼也是 Google,去掉了標(biāo)簽和標(biāo)簽)
20、truncate
截取,截取字符串開始的一段.默認(rèn)是80個(gè),你可以指定第二個(gè)參數(shù)作為在截取的那段字符串后加上什么字符,默認(rèn)情況下,smarty會(huì)截取到一個(gè)詞的末尾,如果你想要精確的截取多少個(gè)字符,把第三個(gè)參數(shù)改為"true" 。
index.php
index.html
<{$str|truncate:10:'...':true}> 結(jié)果為:從前有座山,山...















