• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Smarty中常用变量操作符汇总_PHP

php 搞代码 3年前 (2022-01-25) 33次浏览 已收录 0个评论

本文汇总了Smarty中常用变量操作符,分享给大家供大家参考。具体如下:

php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
语法中使用”|”应用变量操作符,多个参数用”:”??指簟?/DIV>

capitalize[首字母大写]
count_characters[计算字符数]
cat[连接字符串]
count_paragraphs[计算段落数]
count_sentences[计算句数]
count_words[计算词数]
date_format[时间格式]
default[默认]
escape[转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成
]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
组合使用多个操作符

实例如下:

{* 标题大写 *}<br /><h2>{$title|upper}</h2><br />{* 取其前40个字符 *}<br />Topic: {$topic|truncate:40:"..."}<br />{* 格式化文字串 *}<br />{"now"|date_format:"%Y/%m/%d"}<br />{* 在自定义函数里应用调节器 *}<br />{mailto|upper address="[email protected]"}<br />capitalize(首字母大写)

index.php页面如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');<br />$smarty->display('index.tpl');

index.tpl页面如下:

{$articleTitle}<br />{$articleTitle|capitalize}

OUTPUT输出如下:

Police begin campaign to rundown jaywalkers.<br />Police Begin Campaign To Rundown Jaywalkers.

count_characters(计算变量里的字符数)

index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');<br />$smarty->display('index.tpl');

index.tpl页面如下:

{$articleTitle}<br />{$articleTitle|count_characters}

OUTPUT输出如下:

Cold Wave Linked to Temperatures.

cat(连接字符串)
将cat里的值连接到给定的变量后面
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Psychics predict world didn't end');<br />$smarty->display('index.tpl');

index.tpl页面如下:

{$articleTitle|cat:" yesterday."}

OUTPUT输出如下:

Psychics predict world didn't end yesterday.

count_paragraphs(计算段数)
计算变量里的段落数量
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');<br />$smarty->display('index.tpl');

index.tpl模板页面如下:

{$articleTitle}<br />{$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;<br />$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');<br />$smarty->display('index.tpl');

index.tpl模板如下:

{$articleTitle}<br />{$articleTitle|count_sentences}

OUTPUT输出如下:

Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.<br />2

count_words(计算词数)
计算变量里的词数
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');<br />$smarty->display('index.tpl');

index.tpl模板如下:

{$articleTitle}<br />{$articleTitle|count_words}

OUTPUT输出如下:

Dealers Will Hear Car Talk at Noon.<br />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.
输入为空时的默认设置
在给定的函数serftime();里格式日期和时间.
Unix或者mysql等的时间戳(parsable by strtotime)都可以传递到smarty.
设计者可以使用date_format完全控制日期格式.
如果传给date_format的数据是空的,将使用第二个参数作为时间格式
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('yesterday', strtotime('-1 day'));<br />$smarty->display('index.tpl');

index.tpl:

{$smarty.now|date_format}<br />{$smarty.now|date_format:"%A, %B %e, %Y"}<br />{$smarty.now|date_format:"%H:%M:%S"}<br />{$yesterday|date_format}<br />{$yesterday|date_format:"%A, %B %e, %Y"}<br />{$yesterday|date_format:"%H:%M:%S"}

OUTPUT输出如下:

Feb 6, 2001<br />Tuesday, February 6, 2001<br />14:33:00<br />Feb 5, 2001<br />Monday, February 5, 2001<br />14:33:00<br />default(默认)<br />Parameter Position Type Required Default Description <br />1 string No empty This is the default value to output if the variable is empty.

这是变量为空的时候的默认输出
为空变量设置一个默认值.
当变量为空或者未分配的时候,将由给定的默认值替代输出.
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle|default:"no title"}<br />{$myTitle|default:"no title"}

OUTPUT输出:

Dealers Will Hear Car Talk at Noon.<br />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;<br />$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|escape}<br />{$articleTitle|escape:"html"} {* escapes & " '  *}<br />{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}<br />{$articleTitle|escape:"url"}<br />{$articleTitle|escape:"quotes"}<br />href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}

OUTPUT输出:

'Stiff Opposition Expected to Casketless Funeral Plan'<br />'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'<br />'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'<br />'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'<br />'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan'<br />'Stiff Opposition Expected to Casketless Funeral Plan'<br />href="[email protected]:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net

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;<br />$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br /> <br />{$articleTitle|indent}<br /> <br />{$articleTitle|indent:10}<br /> <br />{$articleTitle|indent:1:"t"}

OUTPUT输出:

NJ judge to rule on nude beach.<br />Sun or rain expected today, dark tonight.<br />Statistics show that teen pregnancy drops off significantly after 25.<br /> <br />NJ judge to rule on nude beach.<br />Sun or rain expected today, dark tonight.<br />Statistics show that teen pregnancy drops off significantly after 25.<br />NJ judge to rule on nude beach.<br />Sun or rain expected today, dark tonight.<br />Statistics show that teen pregnancy drops off significantly after 25.<br /> <br />NJ judge<strong>)本文来(源gaodai#ma#com搞@@代~&码*网2</strong><pre>搞代gaodaima码

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;<br />$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|lower}

OUTPUT输出:

Two Convicts Evade Noose, Jury Hung.<br />two convicts evade noose, jury hung.

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

$smarty = new Smarty;<br />$smarty->assign('articleTitle', "Sun or rain expectedntoday, dark tonight");<br />$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;<br />$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");<br />$smarty->display('index.tpl');

index.tpl模板:

{* replace each carriage return, tab & new line with a space *}{* 使用空格替换每个回车,tab,和换行符 *}<br />{$articleTitle}<br />{$articleTitle|regex_replace:"/[rtn]/":" "}

OUTPUT输出:

Infertility unlikely to<br />be passed on, experts say.<br />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;<br />$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|replace:"Garden":"Vineyard"}<br />{$articleTitle|replace:" ":" "}

OUTPUT输出:

Child's Stool Great for Use in Garden.<br />Child's Stool Great for Use in Vineyard.<br />Child's Stool Great for Use in Garden.

spacify
是一种在字符串的每个字符之间插入空格或者插入其他的字符(串).
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|spacify}<br />{$articleTitle|spacify:"^^"}

OUTPUT输出:

Something Went Wrong in Jet Crash, Experts Say.<br />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 .<br />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;<br />$smarty->assign('number', 23.5787446);<br />$smarty->display('index.tpl');

index.tpl模板:

{$number}<br />{$number|string_format:"%.2f"}<br />{$number|string_format:"%d"}

OUTPUT输出:

23.5787446<br />23.58<br />24

strip(去除(多余空格)
替换所有重复的空格,换行和tab为单个.
index.php如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|strip}<br />{$articleTitle|strip:" "}

OUTPUT输出:

Grandmother of<br />eight makes hole in one.<br />Grandmother of eight makes hole in one.<br />Grandmother of eight makes hole in one.

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

$smarty = new Smarty;<br />$smarty->assign('articleTitle', "Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|strip_tags}

OUTPUT输出:

Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.<br />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;<br />$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|truncate}<br />{$articleTitle|truncate:30}<br />{$articleTitle|truncate:30:""}<br />{$articleTitle|truncate:30:"---"}<br />{$articleTitle|truncate:30:"":true}<br />{$articleTitle|truncate:30:"...":true}

OUTPUT输出:

Two Sisters Reunite after Eighteen Years at Checkout Counter.<br />Two Sisters Reunite after Eighteen Years at Checkout Counter.<br />Two Sisters Reunite after...<br />Two Sisters Reunite after<br />Two Sisters Reunite after---<br />Two Sisters Reunite after Eigh<br />Two Sisters Reunite after E...

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

$smarty = new Smarty;<br />$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|upper}

OUTPUT输出:

If Strike isn't Settled Quickly it may Last a While.<br />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;<br />$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|wordwrap:30}<br />{$articleTitle|wordwrap:20}<br />{$articleTitle|wordwrap:30:"<br>n"}<br />{$articleTitle|wordwrap:30:"n":true}

OUTPUT输出:

Blind woman gets new kidney from dad she hasn't seen in years.<br />Blind woman gets new kidney<br />from dad she hasn't seen in<br />years.<br />Blind woman gets new<br />kidney from dad she<br />hasn't seen in<br />years.<br />Blind woman gets new kidney<br><br />from dad she hasn't seen in years.<br />Blind woman gets new kidney fr<br />om dad she hasn't seen in year<br />s.

组合使用多个操作符
可以在一个变量上应用操作符,它们将从左到右依次组合应用.多个操作符必须用”|”符号分开.
index.php页面如下:

$smarty = new Smarty;<br />$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');<br />$smarty->display('index.tpl');

index.tpl模板:

{$articleTitle}<br />{$articleTitle|upper|spacify}<br />{$articleTitle|lower|spacify|truncate}<br />{$articleTitle|lower|truncate:30|spacify}<br />{$articleTitle|lower|spacify|truncate:30:". . ."}

OUTPUT输出:

Smokers are Productive, but Death Cuts Efficiency.<br />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 .<br />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...<br />s m o k e r s a r e p r o d u c t i v e , b u t . . .<br />s m o k e r s a r e p. . .

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Smarty中常用变量操作符汇总_PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址