Smarty(变量修饰器)

一、概念

变量修饰器(调节器)可用于变量,自定义函数和字符串。 请使用‘|’符号和修饰器名称应用修饰器。 变量修饰器由赋予的参数值决定其行为。 参数由‘’符号分开。

二、修饰器用法简介

capitalize

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


参数1:带数字的单词是否也头字母大写(Boolean,默认:false)
参数2:设置单词内其他字母是否小写,如"aA" 变成 "Aa"(Boolean,默认:false)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'I want to buy a samsung s8');
$smarty->display('index.tpl');

index.tpl:

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

OUTPUT:

I want to buy a samsung s8
I Want To Buy A Samsung s8
I Want To Buy A Samsung S8

注:lower修饰器将变量值转成小写字母(所有字母),无参数。
upper与之相反将变量值转成大写字母(所有字母),无参数。

cat

连接多个变量。


参数1:需要连接的变量(String)。


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "To be or not to be");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|cat:'it is a question'}

OUTPUT:

To be or not to be
To be or not to be it is a question

count_characters

计算变量内容里有多少个字符。


参数1:计算总数时是否包括空格字符(Boolean,默认: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

count_paragraphs

计算变量内容有多少个段落。


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy!\n\n
                                Yes I am!");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_paragraphs}

OUTPUT:

I am a good guy!
Yes I am!
2

count_sentences

计算变量内容有多少个句子。 每个句子必须以点号、问号或者感叹号结尾。
(./?/!)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.Yes I am! Are you sure? Yes!");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_sentences}

OUTPUT:

I am a good guy.Yes I am! Are you sure? Yes!
4

count_words

用于计算变量内容有多少个单词。


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.Yes I am! Are you sure? Yes!");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_words}

OUTPUT:

I am a good guy.Yes I am! Are you sure? Yes!
12

date_format

将日期和时间格式化成strftime( )的格式。 时间可以是unix的 时间戳, DateTime 对象, mysql时间戳,或者月日年格式的字符串,与PHP函数strtotime( )类似。并且可以对date_format的格式有完全的控制。 如果传递到date_format的时间为空, 而第二个参数传递了值,那么第二个参数将作为需要格式化的时间。


参数1:输出时间的格式定义(String,默认:%b %e, %Y)
参数2:如果输入为空,则作为默认时间(String,默认:n/a)


index.php:

$smarty = new Smarty;
$config['date'] = '%I:%M %p';
$config['time'] = '%H:%M:%S';
$smarty->assign('config', $config);
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');

index.tpl:

{$smarty.now|date_format}
{$smarty.now|date_format:"%D"}
{$smarty.now|date_format:$config.date}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:$config.time}

OUTPUT:

Jan 1, 2022
01/01/22
02:33 pm
Dec 31, 2021
Monday, December 1, 2021
14:33:00

附:date_format支持格式
W3C DATE_FORMAT( ) 函数

default

为变量设置默认值。 当变量是unset或者empty的字符串时,默认值将显示。 必须要有一个参数。


参数1:当变量为空时输出的值(String)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.Yes I am!");
$smarty->assign('email', '');
$smarty->display('index.tpl');

index.tpl:

{$articleTitle|default:'no title'}
{$myTitle|default:'no title'}
{$email|default:'No email address available'}


OUTPUT:

I am a good guy.Yes I am!
no title
No email address available

escape

escape可用于将变量编码或转换成 html, url, 单引号, 十六进制, 十六进制实体, javascript 和 email地址。 默认是:html。


参数1:这是escape转换后的格式(String,默认:html,可取值:html, htmlall, url, urlpathinfo, 单引号, 十六进制, 十六进制实体, javascript, email地址)
参数2:传递给htmlentities( )的字符集类型(String,默认:UTF-8,可取值:ISO-8859-1, UTF-8, 和其他 htmlentities()支持的字符集)
参数3:两次转换实体,& 到 & (仅在 html 和 htmlall 使用)(Boolean,默认:true)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle',
                "'Stiff Opposition Expected to Casketless Funeral Plan'"
                );
$smarty->assign('EmailAddress','[email protected]');

$smarty->display('index.tpl');

index.tpl & OUTPUT:

{$articleTitle}
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape}
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape:'html'}  
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape:'htmlall'} 
'Stiff Opposition Expected to Casketless Funeral Plan'

click here
click here

{$articleTitle|escape:'quotes'}
\'Stiff Opposition Expected to Casketless Funeral Plan\'

{$EmailAddress|escape:"hexentity"}
{$EmailAddress|escape:'mail'}    {* this converts to email to text *}
bob..snip..et

{'[email protected]'|escape:'mail'}
smarty [AT] example [DOT] com

注:unescape可以解码entity, html 和 htmlall等的编码。 它与escape 修饰器的效果刚好相反。

indent

缩进每一行的字符串,默认是缩进4个空格。 可选的参数可以设置缩进的空格数量。 可选的第二个参数设置缩进使用的字符,如用 "t" 来代替空格缩进。


参数1:设置缩进多少空格(Integer,默认:4)
参数2:设置用什么字符来进行缩进(String,默认:一个空格)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle',
                'NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.'");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}

{$articleTitle|indent}

{$articleTitle|indent:10}

{$articleTitle|indent:1:"\t"}

OUTPUT:

NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.

    NJ judge to rule on nude beach.
    Sun or rain expected today, dark tonight.
    Statistics show that teen pregnancy drops off significantly after 25.

          NJ judge to rule on nude beach.
          Sun or rain expected today, dark tonight.
          Statistics show that teen pregnancy drops off significantly after 25.

        NJ judge to rule on nude beach.
        Sun or rain expected today, dark tonight.
        Statistics show that teen pregnancy drops off significantly after 25.

nl2br

将变量值中的"n"回车全部转换成HTML的
。 等同于PHP的 nl2br()函数。

index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.\nYes I am!")
$smarty->display('index.tpl');

index.tpl:

{$articleTitle|nl2br}

OUTPUT:

I am a good guy.
Yes I am!

regex_replace

用正则表达式搜索和替换变量值。 使用PHP的 preg_replace()函数进行。


参数1:正则表达式(String,默认:n/a)
参数2:替换的字符(String,默认:n/a)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.\nYes I am!")
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}

OUTPUT:

I am a good guy.
Yes I am!
I am a good guy. Yes I am!

注:replace修饰器用法相似,对变量进行简单的搜索和替换。 等同于PHP函数的 str_replace()。

spacify

spacify会在变量的字符串中插入空格。 你可以设置插入的是空格或者别的字符。


参数1:插入字符间的字符(String,默认:一个空格)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say')
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:”^"}

OUTPUT:

Something Went Wrong in Jet Crash, Experts Say.
S o m e t h i n g   W .... snip ....  s h ,   E x p e r t s   S a y .
S^o^m^e^t^h^i^n^g^.... snip .... ^e^r^t^s^ ^S^a^y^.

string_format

格式化字符串,如浮点数等。 使用 sprintf()的PHP函数来进行。


参数1:指定哪种格式 (sprintf)(String,默认:n/a)


index.php:

$smarty = new Smarty;
$smarty->assign('number', 3.1415926);
$smarty->display('index.tpl');

index.tpl:

{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}


OUTPUT:

3.1415926
3.14
3

strip

转换连续空格,回车和tab到单个空格或是指定字符串。
PS.如果希望转换模板文字内的空格,使用内置的 {strip} 函数。

index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', “I am so\ngood that\t    they all like me");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:' '}

OUTPUT:

I am so
good that        they all like me
I am so good that they all like me 
I am so good that they all like me

truncate

截取字符串到指定长度,默认长度是80.


参数1:截取的长度(Integer,默认:80)
参数2:截取后替代显示的字符,该字符长度会被计算到截取长度内(String,默认:…)
参数3:默认truncate会尝试按单词进行截取。 如果你希望按字符截取(单词可能会被截断),需要设置第三个参数TRUE。(Boolean,默认:false)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter');
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
{$articleTitle|truncate:30:'..':true:true}

OUTPUT:

Two Sisters Reunite after Eighteen Years at Checkout Counter
Two Sisters Reunite after Eighteen Years at Checkout Counter
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
Two Sisters Re..ckout Counter

wordwrap

限制一行字符的长度(自动换行),效果与PHP函数wordwrap()一样。


参数1:限定一行的长度(Integer,默认:80)
参数2:换行符号,可自定义换行字符(String,默认:n)
参数3:设置按单词换行(FALSE),或者按字符换行(TRUE),默认情况下,是根据单词来换行的,也就是按英文语法的自动换行。 如果你希望按照字符来换行(边界的单词将拆开),那么可以设置 可选的第三个参数为TRUE


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle’,'Blind woman gets new kidney from dad she hasn't seen in years');
$smarty->display('index.tpl');

index.tpl:

$articleTitle}

{$articleTitle|wordwrap:30}

{$articleTitle|wordwrap:20}

{$articleTitle|wordwrap:30:"
\n"} {$articleTitle|wordwrap:26:"\n":true} OUTPUT: Blind woman gets new kidney from dad she hasn't seen in years Blind woman gets new kidney from dad she hasn't seen in years Blind woman gets new kidney from dad she hasn't seen in years Blind woman gets new kidney
from dad she hasn't seen in
years Blind woman gets new kidn ey from dad she hasn't se en in years

三、复合修饰器

可以联合使用多个修饰器。 它们会按复合的顺序来作用于变量,从左到右。 它们必须以“|” (竖线)进行分隔。

你可能感兴趣的:(smarty,php)