变量调节器用于变量,自定义函数和字符串。使用‘|’符号和调节器名称应用调节器
。变量调节器由赋予的参数至决定其行为。参数由‘:’符号分开。
1,capitalize,将变量里的所有单词首字大写。
2,将cat里的值连接到给定的变量后面
3,count_characters 计算变量里的字符数
例子: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
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->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|count_paragraphs}
输出: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}
输出:2.
7,date_format[格式化日期]
格式化从函数strftime()获得的时间和日期。
Unix或者mysql等的时间戳记(parsable by strtotime)都可以传递到smarty。
设计者可以使用date_format完全控制日期格式。
如果传给date_format的数据是空的,将使用第二个参数作为时间格式。
8,count_words[计算词数]
9,default[默认值]
为空变量设置一个默认值。
当变量为空或者未分配的时候,将由给定的默认值替代输出。
例子:index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}
OUTPUT:
Dealers Will Hear Car Talk at Noon.
no title
10,escape[编码]
用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化
,或者javascript转码。默认是html转码。
indent[缩进]
在每行缩进字符串,默认是4个字符。
作为可选参数,你可以指定缩进字符数。
作为第二个可选参数,你可以指定缩进用什么字符代替。
特别提示:使用缩进时如果是在HTML中,则需要使用& n b s p;(空格)来代替缩进,
否则没有效果。
11,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.