Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

Chapter 5. 变量修饰器

变量修饰器可以用于变量, 自定义函数或者字符串。 使用修饰器,需要在变量的后面加上|(竖线)并且跟着修饰器名称。 修饰器可能还会有附加的参数以便达到效果。 参数会跟着修饰器名称,用:(冒号)分开。 同时,默认全部PHP函数都可以作为修饰器来使用 (不止下面的),而且修饰器可以被 联合使用

Example 5.1. 修饰器例子


{* apply modifier to a variable *}
{$title|upper}

{* modifier with parameters *}
{$title|truncate:40:"..."}

{* apply modifier to a function parameter *}
{html_table loop=$myvar|upper}

{* with parameters *}
{html_table loop=$myvar|truncate:40:"..."}

{* apply modifier to literal string *}
{"foobar"|upper}

{* using date_format to format the current date *}
{$smarty.now|date_format:"%Y/%m/%d"}

{* apply modifier to a custom function *}
{mailto|upper address="smarty@example.com"}

{* using  php's str_repeat *}
{"="|str_repeat:80}

{* php's count *}
{$myArray|@count}

{* this will uppercase and truncate the whole array *}
<select name="name_id">
{html_options output=$my_array|upper|truncate:20}
</select>

  

  • 修饰器可以作用于任何类型的变量,数组或者对象。

    Note

    这是Smarty3的默认行为。在Smarty2.x,你需要在数组后加上"@" 标识来使用修饰器,如{$articleTitle|@count}。 在Smarty3,不再需要使用"@",它会被忽略。

    如果你想要在数组的每一项中都加上修饰器,你可以通过循环数组进行,或者可以 在修饰器函数中提供这个功能。

    Note

    另外,在Smarty2.x,修饰器可以作用在数学表达式的结果上,如{8+2}, 意味着{8+2|count_characters}的结果是2, 因为8+2=10 而 10 是两个字符长度。 在Smarty3,修饰器将会作用在变量上,或者是在表达式计算前,所以因为 2 是一个字符长度, 所以{8+2|count_characters}的结果是9. 如果希望出现原来的结果,可以使用括号,如{(8+2)|count_characters}

  • 修饰器可以从$plugins_dir 目录中自动加载,或者通过registerPlugin() 来进行动态注册。 第二种方法在PHP代码和smarty模板间共享函数时很有用。

  • 默认全部PHP函数都可以作为修饰器,正如上面例子演示的。 然而,使用php函数作为修饰器会存在两个小问题:

    • 首先 - 有时函数参数的顺序并不太一致。如使用 {"%2.f"|sprintf:$foo}来格式化$foo是正确的。 但是更直观的做法,{$foo|string_format:"%2.f"}是Smarty自身的函数。

    • 其次 - 如果开启了安全限制,那么要使用php函数作为修饰器,就必须通过 $modifiers属性来进行设置信任的函数。 参见安全机制的章节。

参见 registerPlugin(), 修饰器组合. 和 Smarty扩展

capitalize

使变量内容里的每个单词的第一个字母大写。 与PHP函数的 ucwords()相似。

参数顺序 类型 必选参数 默认值 说明
1 boolean No FALSE 带数字的单词是否也头字母大写。
2 boolean No FALSE 设置单词内其他字母是否小写,如"aAa" 变成 "Aaa"。

Example 5.2. capitalize


<?php

$smarty->assign('articleTitle', 'next x-men film, x3, delayed.');

?>

   

模板是:


{$articleTitle}
{$articleTitle|capitalize}
{$articleTitle|capitalize:true}

   

输出:


next x-men film, x3, delayed.
Next X-Men Film, x3, Delayed.
Next X-Men Film, X3, Delayed.

   

参见 lowerupper

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 13 plus 10? (Are you human?)

Advertisement