Smarty(变量修饰器)

1、概念

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

2、修饰器用法简介

capitalize

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


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


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与之相反将变量值转成大写字母(全部字母),无参数。java

cat

链接多个变量。mysql


参数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

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


参数1:计算总数时是否包括空格字符(Boolean,默认:false)。api


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

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


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','smarty@example.com');

$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'

<a href="?title={$articleTitle|escape:'url'}">click here</a>
<a href="?title=%27Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan%27">click here</a>

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

<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
{$EmailAddress|escape:'mail'}    {* this converts to email to text *}
<a href="mailto:%62%6f%..snip..%65%74">&#x62;&#x6f;&#x62..snip..&#x65;&#x74;</a>

{'mail@example.com'|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的<br/>。 等同于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.<br/>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:'&nbsp;'}

OUTPUT:

I am so
good that        they all like me
I am so good that they all like me 
I&nbsp;am&nbsp;so&nbsp;good&nbsp;that&nbsp;they&nbsp;all&nbsp;like&nbsp;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:"<br />\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<br />
from dad she hasn't seen in<br />
years

Blind woman gets new kidn
ey from dad she hasn't se
en in years

3、复合修饰器

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

相关文章
相关标签/搜索