您的位置:首页 > 其它

Smarty变量调节器(1)

2012-11-12 15:25 204 查看
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'

'Stiff Opposition Expected to Casketless Funeral Plan'

'Stiff Opposition Expected to Casketless Funeral Plan'

'Stiff Opposition Expected to Casketless Funeral Plan'

%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">bob@me.net</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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: