Smarty变量调节器(1)

1.capitalize(首字母大写):将变量里的所有单词首字大写。
index.php:


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


index.tpl:


{$articleTitle}
{$articleTitle|capitalize}


OUTPUT:


Police begin campain to rundown jaywalkers.
Police Begin Campain To Rundown Jaywalkers.


2.count_characters(字符计数):计算变量里的字符数。有一个可选参数,默认为false决定是否计算空格字符。
index.php:


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


index.tpl:


{$articleTitle}
{$articleTitle|count_characters}
{$articleTitle|count_characters:true}


OUTPUT:


Cold Wave Linked to Temperatures.
29
33


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


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


index.tpl


{$articleTitle|cat:"yesterday."}


OUTPUT:


Psychics predict world didn't end yesterday.


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


$smarty = new Smarty;
$smarty->assign('articleTitle',"War Dims Hope for Peace. Child's Death Ruins
Couple's Holiday.\n\nMan is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.");
$smarty->('index.tpl');


index.tpl:


{$articleTitle}
{$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


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


$smarty = new Smarty;
$smarty->assign('articleTitle','Two Soviet Ships Collide - One Dies.Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');


index.tpl:


{$articleTitle}
{$articleTitle|count_sentences}


OUTPUT:


Two Soviet Ships Collide - One Dies.Enraged Cow Injures Farmer with Axe.
2


6.count_words(计算词数):计算变量里的词数。
index.php


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


index.tpl


{$articleTitle}
{$articleTitle|count_words}


OUTPUT:


Dealers Will Hear Car Talk Talk at Noon.
7


7.date_format(格式化日期):格式化获得的时间和日期。
index.php


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


index.tpl


{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}




OUTPUT:


2012-11-12 15:05:20




8.string_format(字符串格式化):一种格式化字符串方法。例如格式化为十进制数等等。使用sprintf语法格式化。


index.php


$smarty = new Smarty;
$smarty->assign('number',23.123456);
$smarty->display('index.tpl');


index.tpl


{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}


OUTPUT:


23.123456
23.12
23




9.escape(编码):用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码。默认码是html转码。


index.php


$smarty = new Smarty;
$smarty->assign('articleTitle',"'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');


index.tpl


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


OUTPUT:


'Stiff Opposition Expected to Casketless Funeral Plan'
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
%27Stiff+Opposition+Expected+to+Casketless+Funeral+Plan%27
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&#x40;&#x6d;&#x65;&#x2e;&#x6e;&#x65;&#x74;</a>


  10.nl2br(换行符替换成<br/>):将所有换行符替换成<br/>.功能同PHP中的nl2br()函数一样。


index.php


$smarty = new Smarty;
$smarty->assign('artucleTitle',"Sun or rain expected\ntoday, dark tonight");
$smarty->display('index.tpl');


index.tpl


{$articleTitle|nl2br}


OUTPUT:


Sun or rain expected<br/>today,dark tonight







































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值