一、对变量调节器的介绍
变量调节器用于变量,自定义函数和字符串。请使用‘|’符号和调节器名称应用调节器。变量调节器由赋予的参数值决定其行为。参数由‘:’符号分开。
二、调节器
1、capitalize(将变量里的所有单词首字大写)
index.php
$smarty = new Smarty;
$string = "hello world. \n hello everyone!":
$smarty->assign("string",$string);
$smarty->display('index.tpl');
index.tpl:
{$string}
{$string|capitalize}
输出:
hello world. hello everyone!
Hello World. Hello Everyone!
2、count_characters(字符计数)
index.tpl
{$string}总的字符数(不计空格)是:{$string|count_characters}
{$string}总的字符数(计空格)是:{$string|count_characters:true}
输出:
hello world. hello everyone! 的字符个数(不计空格):25
hello world. hello everyone! 的字符个数(计空格):31
3、cat(连接字符)
index.tpl
给字符串$string连接***之后是:{$string|cat:"***!"}
输出:
给字符串hello world. hello everyone! 连接***之后是:hello world. hello everyone! ***
4、count_paragraphs(计算段落)
index.tpl
{$string}段落数:{$string|count_paragraphs}
输出:
hello world. hello everyone!段落数:2
5、count_sentences(计算句数)(以“.”为准)
index.tpl
{$string}总句数为:{$string|count_sentences}
输出:
hello world. hello everyone!总句数是:1
6、count_words(计算词数)(以 “空格”为准)
index.tpl
{$string}总词数为:{$string|count_words}
输出:
hello world. hello everyone!总词数是:4
7、date_format(格式化时间)
index.tpl
当前时间: {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
输出:
当前系统时间为:2012-11-12 16:42:19
[日期转换说明]
%a - abbreviated weekday name according to the current locale(根据当地格式输出“星期”缩写格式)
%A - full weekday name according to the current locale (根据当地格式输出“星期”全称格式)
%b - abbreviated month name according to the current locale (根据当地格式输出“月”缩写格式)
%B - full month name according to the current locale (根据当地格式输出“月”全称格式)
%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
%d - day of the month as a decimal number (range 00 to 31)
%H - hour as a decimal number using a 24-hour clock (range 00 to 23)
%m - month as a decimal number (range 01 to 12)
%M - minute as a decimal number
%S - second as a decimal number
%y - year as a decimal number without a century (range 00 to 99)
%Y - year as a decimal number including the century
%w - day of the week as a decimal, Sunday being 0
%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
8、default[默认值]
{$string|default:"no title"}
9、escape[编码]
用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码。默认是html转码。
10、indent[缩进]
有俩个参数:
第一个参数是:决定缩进多少个字符
第二个参数是:使用什么字符来代替缩进
<{$string|indent:10:" "}>
输出: hello world. hello everyone!
11、lower(将变量字符串小写)
$string=Hello WOrld
{$string|lower}
输出: hello world
12、regex_replace(正则替换)
有俩个参数:
第一个参数是:替换正则表达式
第二个参数是:用来替换的文本字符
13、replace(替换)
第一个参数是:将被替换的文本字串
第二个参数是:用来替换的文本字串
例如:
$cap = "hello word! hello everyone":
<{$cap|replace:"hello":"hi"}>
输出: hi world! hi everyone!
14、string_format(字符串格式化)
$num=123.12345
<{$num}>通过%m(m表示整数)d格式化:
<{$num|string_format:"%d"}><br />
<{$num}>通过%m.nf()格式化一个实数:
<{$num|string_format:"%6.2f"}>
<{$num|string_format:"%.3f"}><br />
<{$num}>通过%e(科学计数法表示)格式化一个实数:
<{$num|string_format:"%e"}>
<{$num|string_format:"%3.1e"}>
输出:
123.12345通过%m(m表示共有多少字符)d格式化:123
123.12345通过%m.nf格式化一个实数:123.12 123.123
123.12345通过%e格式化一个实数:1.231235e+2 1.2e+2
15、strip去除(多余空格)
用一个空格或一个给定字符替换所有重复空格,换行和制表符.
16、truncate(截取)
Parameter Position参数位置 | Type参数类型 | Required必需 | Default默认 | Description描述 |
---|---|---|---|---|
1 | integer | No | 80 | 截取字符的数量 |
2 | string | No | ... | 截取后追加在截取词后面的字符串 |
3 | boolean | No | false | 是截取到词的边界(假)还是精确到字符(真) |
17、upper将变量改为大写
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
{$articleTitle|upper}
输出:
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
18、spacify(插空是一种在字符串的每个字符之间插入空格或其他字符(串))
Parameter Position参数位置 | Type参数类型 | Required必需 | Default默认 | Description描述 |
---|---|---|---|---|
1 | string | No | one space | This what gets inserted between each character of the variable. 将在两个字符之间插入的字符(串) |
<{$cap|spacify}>
<{$cap|spacify:"*"}>
输出:
h e l l o w o r l d . h e l l o e v e r y o n e ! h e l l o p h p
h*e*l*l*o* *w*o*r*l*d*.* ** *h*e*l*l*o* *e*v*e*r*y*o*n*e*!* *h*e*l*l*o* *p*h*p
19、组合修改器
对于同一个变量,你可以使用多个修改器,他们将从左到右按照设定好的顺序被依次组合。使用时必须要用"|"字符作为它们之间的分隔符。
实例:<{$cap|upper|spacify}>
输出:H E L L O W O R L D . H E L L O E V E R Y O N E ! H E L L O P H P