zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Smarty中常用变量操作符汇总

变量 常用 汇总 操作符 smarty
2023-06-13 09:15:29 时间

本文汇总了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

count_sentences(计算句数)
计算变量里句子的数量
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","TwoSovietShipsCollide-OneDies.EnragedCowInjuresFarmerwithAxe.");
$smarty->display("index.tpl");

index.tpl模板如下:

复制代码代码如下:{$articleTitle}
{$articleTitle|count_sentences}

OUTPUT输出如下:

复制代码代码如下:TwoSovietShipsCollide-OneDies.EnragedCowInjuresFarmerwithAxe.
2
count_words(计算词数)
计算变量里的词数
index.php如下:
复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","DealersWillHearCarTalkatNoon.");
$smarty->display("index.tpl");

index.tpl模板如下:

复制代码代码如下:{$articleTitle}
{$articleTitle|count_words}

OUTPUT输出如下:

复制代码代码如下:DealersWillHearCarTalkatNoon.
7
date_format(日期格式)
ParameterPosition
参数位置TypeRequiredDefaultDescription
1stringNo%b%e,%YThisistheformatfortheoutputteddate.
输出字串的格式
2stringNon/aThisisthedefaultdateiftheinputisempty.
输入为空时的默认设置
在给定的函数serftime();里格式日期和时间.
Unix或者mysql等的时间戳(parsablebystrtotime)都可以传递到smarty.
设计者可以使用date_format完全控制日期格式.
如果传给date_format的数据是空的,将使用第二个参数作为时间格式
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("yesterday",strtotime("-1day"));
$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输出如下:

复制代码代码如下:Feb6,2001
Tuesday,February6,2001
14:33:00
Feb5,2001
Monday,February5,2001
14:33:00
default(默认)
ParameterPositionTypeRequiredDefaultDescription
1stringNoemptyThisisthedefaultvaluetooutputifthevariableisempty.
这是变量为空的时候的默认输出
为空变量设置一个默认值.
当变量为空或者未分配的时候,将由给定的默认值替代输出.
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","DealersWillHearCarTalkatNoon.");
$smarty->display("index.tpl");

index.tpl模板:

复制代码代码如下:{$articleTitle|default:"notitle"}
{$myTitle|default:"notitle"}

OUTPUT输出:

复制代码代码如下:DealersWillHearCarTalkatNoon.
notitle
escape(转码)
ParameterPositionTypeRequiredPossibleValuesDefaultDescription
1stringNohtml,htmlall,url,quotes,hex,hexentity,javascripthtmlThisistheescapeformattouse.
用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码.
默认是html转码
index.php如下:
 
复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle",""StiffOppositionExpectedtoCasketlessFuneralPlan"");
$smarty->display("index.tpl");
 
index.tpl模板:
 
复制代码代码如下:{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"}{*escapes&""<>*}
{$articleTitle|escape:"htmlall"}{*escapesALLhtmlentities*}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a
href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
 
OUTPUT输出:
 
复制代码代码如下:"StiffOppositionExpectedtoCasketlessFuneralPlan"
"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"
"StiffOppositionExpectedtoCasketlessFuneralPlan"
<a
href="bob@me.netmailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net</a>
indent(缩进)
ParameterPositionTypeRequiredDefaultDescription
1integerNo4Thisdetermineshowmanycharacterstoindentto.
2stringNo(onespace)Thisisthecharacterusedtoindentwith.
在每行缩进字符串,默认是4个字符(pear标准也是).
作为可选参数,你可以指定缩进字符数.
作为第二个可选参数,你可以指定缩进用什么字符代替
index.php如下:
 
复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","NJjudgetoruleonnudebeach.");
$smarty->display("index.tpl");
 
index.tpl模板:
 
复制代码代码如下:{$articleTitle}
 
{$articleTitle|indent}
 
{$articleTitle|indent:10}
 
{$articleTitle|indent:1:"t"}
 
OUTPUT输出:
 
复制代码代码如下:NJjudgetoruleonnudebeach.
Sunorrainexpectedtoday,darktonight.
Statisticsshowthatteenpregnancydropsoffsignificantlyafter25.
 
NJjudgetoruleonnudebeach.
Sunorrainexpectedtoday,darktonight.
Statisticsshowthatteenpregnancydropsoffsignificantlyafter25.
NJjudgetoruleonnudebeach.
Sunorrainexpectedtoday,darktonight.
Statisticsshowthatteenpregnancydropsoffsignificantlyafter25.
 
NJjudgetoruleonnudebeach.
Sunorrainexpectedtoday,darktonight.
Statisticsshowthatteenpregnancydropsoffsignificantlyafter25.

lower(小写)
将变量字符串小写
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","TwoConvictsEvadeNoose,JuryHung.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|lower}
OUTPUT输出:
复制代码代码如下:TwoConvictsEvadeNoose,JuryHung.
twoconvictsevadenoose,juryhung.

nl2br(换行符替换成<br/>)
所有的换行符将被替换成<br/>.同php的nl2br()函数一样.
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","Sunorrainexpectedntoday,darktonight");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle|nl2br}
OUTPUT输出:
复制代码代码如下:Sunorrainexpected<br/>today,darktonight

regex_replace(正则替换)
寻找和替换正则表达式.
ParameterPositionTypeRequiredDefaultDescription
1stringYesn/aThisistheregularexpressiontobereplaced.
替换正则表达式.

2stringYesn/aThisisthestringoftexttoreplacewith.
使用什么文本字串来替换
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","Infertilityunlikelytonbepassedon,expertssay.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{*replaceeachcarriagereturn,tab&newlinewithaspace*}{*使用空格替换每个回车,tab,和换行符*}
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":""}
OUTPUT输出:
复制代码代码如下:Infertilityunlikelyto
bepassedon,expertssay.
Infertilityunlikelytobepassedon,expertssay.

replace(替换)
简单的搜索和替换字符串
ParameterPositionTypeRequiredDefaultDescription
1stringYesn/aThisisthestringoftexttobereplaced.
将被替换的字符串
2stringYesn/aThisisthestringoftexttoreplacewith.
用来替换的文本
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","Child"sStoolGreatforUseinGarden.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:"":""}
OUTPUT输出:
复制代码代码如下:Child"sStoolGreatforUseinGarden.
Child"sStoolGreatforUseinVineyard.
Child"sStoolGreatforUseinGarden.
spacify
是一种在字符串的每个字符之间插入空格或者插入其他的字符(串).
index.php如下:
复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","SomethingWentWronginJetCrash,ExpertsSay.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT输出:
复制代码代码如下:SomethingWentWronginJetCrash,ExpertsSay.
SomethingWentWronginJetCrash,ExpertsSay.
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(字符串格式化)
ParameterPositionTypeRequiredDefaultDescription
1stringYesn/aThisiswhatformattouse.(sprintf)
使用的格式化方式
是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$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=newSmarty;
$smarty->assign("articleTitle","Grandmotherofneightmakestholeinone.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}
OUTPUT输出:
复制代码代码如下:Grandmotherof
eightmakesholeinone.
Grandmotherofeightmakesholeinone.
Grandmother of eight makes hole in one.

strip_tags(去除html标签)
去除在<和>之间的所有标签,包括<和>.
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","BlindWomanGets<fontface="helvetica">NewKidney</font>fromDadsheHasn"tSeenin<b>years</b>.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|strip_tags}
OUTPUT输出:
复制代码代码如下:BlindWomanGets<fontface="helvetica">NewKidney</font>fromDadsheHasn"tSeenin<b>years</b>.
BlindWomanGetsNewKidneyfromDadsheHasn"tSeeninyears.
truncate(截取)
ParameterPositionTypeRequiredDefaultDescription
1integerNo80Thisdetermineshowmanycharacterstotruncateto.
指定截取多少字符
2stringNo...Thisisthetexttoappendiftruncationoccurs.
截取后加在截取词后的字符串
3booleanNofalseThisdetermineswhetherornottotruncateatawordboundary(false),orattheexactcharacter(true).
检查是否截取到词的边界
截取字符串开始的一段.默认是80个.
你可以指定第二个参数作为在截取的那段字符串后加上什么字符.
默认情况下,smarty会截取到一个词的末尾,
如果你想要精确的截取多少个字符,把第三个参数改为"true"
index.php如下:
复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","TwoSistersReuniteafterEighteenYearsatCheckoutCounter.");
$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输出:
复制代码代码如下:TwoSistersReuniteafterEighteenYearsatCheckoutCounter.
TwoSistersReuniteafterEighteenYearsatCheckoutCounter.
TwoSistersReuniteafter...
TwoSistersReuniteafter
TwoSistersReuniteafter---
TwoSistersReuniteafterEigh
TwoSistersReuniteafterE...

upper(大写)
将变量改为大写
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","IfStrikeisn"tSettledQuicklyitmayLastaWhile.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|upper}
OUTPUT输出:
复制代码代码如下:IfStrikeisn"tSettledQuicklyitmayLastaWhile.
IFSTRIKEISN"TSETTLEDQUICKLYITMAYLASTAWHILE.

wordwrap(行宽约束)
可以指定段落的宽度(也就是多少个字符一行,超过这个字符数换行).默认80.
第二个参数可选,可以指定在约束点使用什么字符(默认是换行符n).
默认情况下smarty将截取到词尾,你也可以指定精确截取多少个字符
ParameterPositionTypeRequiredDefaultDescription
1integerNo80Thisdetermineshowmanycolumnstowrapto.
指定段落(句子)的宽度
2stringNonThisisthestringusedtowrapwordswith.
使用什么字符约束
3booleanNofalseThisdetermineswhetherornottowrapatawordboundary(false),orattheexactcharacter(true).
是否精确约束到字符
index.php如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","Blindwomangetsnewkidneyfromdadshehasn"tseeninyears.");
$smarty->display("index.tpl");
index.tpl模板:
复制代码代码如下:{$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"<br>n"}
{$articleTitle|wordwrap:30:"n":true}
OUTPUT输出:
复制代码代码如下:Blindwomangetsnewkidneyfromdadshehasn"tseeninyears.
Blindwomangetsnewkidney
fromdadshehasn"tseenin
years.
Blindwomangetsnew
kidneyfromdadshe
hasn"tseenin
years.
Blindwomangetsnewkidney<br>
fromdadshehasn"tseeninyears.
Blindwomangetsnewkidneyfr
omdadshehasn"tseeninyear
s.
组合使用多个操作符
可以在一个变量上应用操作符,它们将从左到右依次组合应用.多个操作符必须用"|"符号分开.
index.php页面如下:

复制代码代码如下:$smarty=newSmarty;
$smarty->assign("articleTitle","SmokersareProductive,butDeathCutsEfficiency.");
$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输出:

复制代码代码如下:SmokersareProductive,butDeathCutsEfficiency.
SMOKERSAREPRODUCTIVE,BUTDEATHCUTSEFFICIENCY.
smokersareproductive,butdeathcuts...
smokersareproductive,but...
smokersarep...

希望本文所述对大家的PHP程序设计有所帮助。