Smarty变量调节器2

来源:互联网 发布:云盘 mac 编辑:程序博客网 时间:2024/05/22 02:10
1.indent(缩进):在每行缩进字符串,默认是4个字符。第一个参数可选,你可以


指定缩进字符数。第二个可选参数,你可以指定缩进用什么字符代替。特别提示:使用缩进


时如果是在HTML中,则需要使用& n b s p;(空格)来代替缩进,否则没有效果。


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}


{$articleTitle|indent}


{$articleTitle|indent:10}


{$articleTitle|indent:1:"\t"}


OUTPUT:


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.


NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
 
2.lower(字母小写):将变量字符创小写


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}
{$articleTitle|lower}


OUTPUT:


Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.




3.regex_replace(正则替换):寻找和替换正则表达式 


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely to\nbe passed on, experts 


say.");
$smarty->display('index.tpl');


index.tpl:


{* replace each carriage return, tab & new line with a space *}{* 使用空格替换每


个回车,tab,和换行符 *}
{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}


OUTPUT:


Infertility unlikely to
 be passed on, experts say.
Infertility unlikely to be passed on, experts say.




4.replace(替换):简单的搜索和替换


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}


OUTPUT:


Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.




5.spacify(插空):顾名思义是一种在字符串的每个字符之间插入空格或者其他的


字符(串).


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts 


Say.');
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}


OUTPUT:


Something Went Wrong in Jet Crash, Experts Say.
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^^.




6.strip(去除(多余空格)):用一个空格或一个给定字符替换所有重复空格,换行


和制表符.


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother of\neight makes\t hole in one.");
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}


OUTPUT:


Grandmother of
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.




7.strip_tags(去除html标签):去除<和>标签,包括在<和>之间的任何内容.


index.php:


$smarty = new Smarty;
$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}
{$articleTitle|strip_tags}


OUTPUT:


Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't 


Seen in <b>years</b>.
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.




8.truncate(截取):从字符串开始处截取某长度的字符.默认是80个.你也可以指定


第二个参数作为追加在截取字符串后面的文本字串.该追加字串被计算在截取长度中。默认


情况下,smarty会截取到一个词的末尾。如果你想要精确的截取多少个字符,把第三个参数改


为"true" 。


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at 


Checkout Counter.');
$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:


Two Sisters Reunite after Eighteen Years at Checkout Counter.
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...




9.wordwrap(行宽约束):可以指定段落的宽度(也就是多少个字符一行,超过这个字


符数换行).默认80.第二个参数可选,可以指定在约束点使用什么字符(默认是换行符\n).默


认情况下smarty将截取到词尾,如果想精确到设定长度的字符,请将第三个参数设为ture.


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't 


seen in years.");
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}


{$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 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.




10.upper(字母大写):将变量改为大写


index.php:


$smarty = new Smarty;
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a 


While.");
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}
{$articleTitle|upper}


OUTPUT:


If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
 

原创粉丝点击