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|upper address="main@cn-web.com"}
capitalize(首字母大写)
index.php页面如下:
$smarty->assign('articletitle', 'police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
index.tpl页面如下:
{$articletitle|capitalize}
output输出如下:
police begin campaign to rundown jaywalkers.
count_characters(计算变量里的字符数)
index.php如下:
$smarty->assign('articletitle', 'cold wave linked to temperatures.');
$smarty->display('index.tpl');
index.tpl页面如下:
{$articletitle|count_characters}
output输出如下:
cold wave linked to temperatures.
cat(连接字符串)
将cat里的值连接到给定的变量后面
index.php如下:
$smarty->assign('articletitle', 'psychics predict world didn't end');
$smarty->display('index.tpl');
index.tpl页面如下:
output输出如下:
count_paragraphs(计算段数)
计算变量里的段落数量
index.php如下:
$smarty->assign('articletitle', 'war dims hope for peace. child's death ruins couple's holiday.');
$smarty->display('index.tpl');
index.tpl模板页面如下:
{$articletitle|count_paragraphs}
output输出如下:
man is fatally slain. death causes loneliness, feeling of isolation.
2
count_sentences(计算句数)
计算变量里句子的数量
index.php如下:
$smarty->assign('articletitle', 'two soviet ships collide - one dies. enraged cow injures farmer with axe.');
$smarty->display('index.tpl');
index.tpl模板如下:
{$articletitle|count_sentences}
output输出如下:
2
count_words(计算词数)
计算变量里的词数
index.php如下:
$smarty->assign('articletitle', 'dealers will hear car talk at noon.');
$smarty->display('index.tpl');
index.tpl模板如下:
{$articletitle|count_words}
output输出如下:
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->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
index.tpl:
{$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输出如下:
tuesday, february 6, 2001
14:33:00
feb 5, 2001
monday, february 5, 2001
14:33:00
default(默认)
parameter position type required default description
1 string no empty this is the default value to output if the variable is empty.
这是变量为空的时候的默认输出
为空变量设置一个默认值.
当变量为空或者未分配的时候,将由给定的默认值替代输出.
index.php如下:
$smarty->assign('articletitle', 'dealers will hear car talk at noon.');
$smarty->display('index.tpl');
index.tpl模板:
{$mytitle|default:"no title"}
output输出:
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->assign('articletitle', "'stiff opposition expected to casketless funeral plan'");
$smarty->display('index.tpl');
index.tpl模板:
{$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输出:
'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(缩进)
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->assign('articletitle', 'nj judge to rule on nude beach.');
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|indent}
{$articletitle|indent:10}
{$articletitle|indent:1:"t"}
output输出:
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如下:
$smarty->assign('articletitle', 'two convicts evade noose, jury hung.');
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|lower}
output输出:
two convicts evade noose, jury hung.
nl2br(换行符替换成<br /> )
所有的换行符将被替换成 <br />.同php的nl2br()函数一样.
index.php如下:
$smarty->assign('articletitle', "sun or rain expectedntoday, dark tonight");
$smarty->display('index.tpl');
index.tpl模板:
output输出:
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->assign('articletitle', "infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle}
{$articletitle|regex_replace:"/[rtn]/":" "}
output输出:
be passed on, experts say.
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->assign('articletitle', "child's stool great for use in garden.");
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|replace:"garden":"vineyard"}
{$articletitle|replace:" ":" "}
output输出:
child's stool great for use in vineyard.
child's stool great for use in garden.
spacify
是一种在字符串的每个字符之间插入空格或者插入其他的字符(串).
index.php如下:
$smarty->assign('articletitle', 'something went wrong in jet crash, experts say.');
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|spacify}
{$articletitle|spacify:"^^"}
output输出:
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)
使用的格式化方式
是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化
index.php如下:
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
index.tpl模板:
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
output输出:
23.58
24
strip(去除(多余空格)
替换所有重复的空格,换行和tab为单个.
index.php如下:
$smarty->assign('articletitle', "grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|strip}
{$articletitle|strip:" "}
output输出:
eight makes hole in one.
grandmother of eight makes hole in one.
grandmother of eight makes hole in one.
strip_tags(去除html标签)
去除在<和>之间的所有标签,包括<和>.
index.php如下:
$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模板:
{$articletitle|strip_tags}
output输出:
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->assign('articletitle', 'two sisters reunite after eighteen years at checkout counter.');
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|truncate}
{$articletitle|truncate:30}
{$articletitle|truncate:30:""}
{$articletitle|truncate:30:"---"}
{$articletitle|truncate:30:"":true}
{$articletitle|truncate:30:"...":true}
output输出:
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->assign('articletitle', "if strike isn't settled quickly it may last a while.");
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|upper}
output输出:
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->assign('articletitle', "blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');
index.tpl模板:
{$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
kidney from dad she
hasn't seen in
years.
blind woman gets new kidney<br>
from dad she hasn't seen in years.
blind woman gets new kidney fr
om dad she hasn't seen in year
s.
组合使用多个操作符
可以在一个变量上应用操作符,它们将从左到右依次组合应用.多个操作符必须用"|"符号分开.
index.php页面如下:
$smarty->assign('articletitle', 'smokers are productive, but death cuts efficiency.');
$smarty->display('index.tpl');
index.tpl模板:
{$articletitle|upper|spacify}
{$articletitle|lower|spacify|truncate}
{$articletitle|lower|truncate:30|spacify}
{$articletitle|lower|spacify|truncate:30:". . ."}
output输出:
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. . .
希望本文所述对大家的php程序设计有所帮助。