Chapter 5. Variable Modifiers[变量调节器]

Table of Contents
capitalize[首字大写]
count_characters[字符计数]
cat[连接字符串]
count_paragraphs
count_sentences
count_words
date_format
default
escape
indent
lower
nl2br
regex_replace
replace
spacify
string_format
strip
strip_tags
truncate
upper
wordwrap

Variable modifiers can be applied to variables, custom functions or strings.
To apply a modifier, specify the value followed by the | (pipe) and the modifier name.
A modifier may accept additional parameters that affect its behavior.
These parameters follow the modifer name and are separated by : (colon).

变量调节器用于变量,自定义函数和字符串.
请使用'|'符号和调节器名称应用调节器.
变量调节器由赋予的参数值决定其行为.
参数由':'符号分开.

Example 5-1. modifier example
例 5-1.调节器的例子

{* Uppercase the title *}
{* 标题大写 *} <h2>{$title|upper}</h2> {* Truncate the topic to 40 characters use ... at the end *} {* 取其前40个字符 *} Topic: {$topic|truncate:40:"..."} {* format a literal string *} {* 格式化文字串 *} {"now"|date_format:"%Y/%m/%d"} {* apply modifier to a custom function *} {* 在自定义函数里应用调节器 *} {mailto|upper address="me@domain.dom"}

If you apply a modifier to an array variable instead of a single value variable, the modifier will be applied to every value in that array.
If you really want the modifier to work on an entire array as a value, you must prepend the modifier name with an @ symbol like so: {$articleTitle|@count} (this will print out the number of elements in the $articleTitle array.)

如果你给数组变量应用单值变量的调节,结果是数组的每个值都被调节.
如果你只想要调节器用一个值调节整个数组,你必须在调节器名字前加上@符号.
例如: {$articleTitle|@count}(这将会在 $articleTitle 数组里输出元素的数目)????

capitalize
首字大写

This is used to capitalize the first letter of all words in a variable.

将变量里的所有单词首字大写.

Example 5-2. capitalize
例 5-2.首字大写

index.php:

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

index.tpl:

{$articleTitle}
{$articleTitle|capitalize}

OUTPUT:

Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.