Smarty中常用变量操作符汇总
本文汇总了Smarty中常用变量操作符,分享给大家供大家参考。具体如下:
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> {*取其前40个字符*} Topic:{$topic|truncate:40:"..."} {*格式化文字串*} {"now"|date_format:"%Y/%m/%d"} {*在自定义函数里应用调节器*} {mailto|upperaddress="main@cn-web.com"} capitalize(首字母大写)
index.php页面如下:
$smarty=newSmarty; $smarty->assign('articleTitle','Policebegincampaigntorundownjaywalkers.'); $smarty->display('index.tpl');
index.tpl页面如下:
{$articleTitle} {$articleTitle|capitalize}
OUTPUT输出如下:
Policebegincampaigntorundownjaywalkers. PoliceBeginCampaignToRundownJaywalkers.
count_characters(计算变量里的字符数)
index.php如下:
$smarty=newSmarty; $smarty->assign('articleTitle','ColdWaveLinkedtoTemperatures.'); $smarty->display('index.tpl');
index.tpl页面如下:
{$articleTitle} {$articleTitle|count_characters}
OUTPUT输出如下:
ColdWaveLinkedtoTemperatures.
cat(连接字符串)
将cat里的值连接到给定的变量后面
index.php如下:
$smarty=newSmarty; $smarty->assign('articleTitle','Psychicspredictworlddidn'tend'); $smarty->display('index.tpl');
index.tpl页面如下:
{$articleTitle|cat:"yesterday."}
OUTPUT输出如下:
Psychicspredictworlddidn'tendyesterday.
count_paragraphs(计算段数)
计算变量里的段落数量
index.php如下:
$smarty=newSmarty; $smarty->assign('articleTitle','WarDimsHopeforPeace.Child'sDeathRuinsCouple'sHoliday.'); $smarty->display('index.tpl');
index.tpl模板页面如下:
{$articleTitle} {$articleTitle|count_paragraphs}
OUTPUT输出如下:
WarDimsHopeforPeace.Child'sDeathRuinsCouple'sHoliday.
ManisFatallySlain.DeathCausesLoneliness,FeelingofIsolation. 2