当前位置: 首页 > 后端技术 > PHP

Smarty(变量装饰器)

时间:2023-03-29 17:03:52 PHP

1.概念变量装饰器(调节器)可用于变量、自定义函数和字符串。请使用'|'符号和装饰器名称以应用装饰器。变量装饰器的行为由分配给它的参数值决定。参数由“:”符号分隔。2.修饰语用法介绍capitalize将变量内容中每个单词的首字母大写。类似于PHP函数ucwords()。参数1:带数字的单词是否也大写(Boolean,默认:false)参数2:设置单词中其他字母是否小写,如“aA”变为“Aa”(Boolean,默认:false)index.php:$smarty=newSmarty;$smarty->assign('articleTitle','我想买三星s8');$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|capitalize}{$articleTitle|capitalize:true}OUTPUT:Iwanttobuyasamsungs8IWantToBuyASamsungs8IWanttoBuyASamsungS8注意:lower修饰符将变量值转换为小写字母(所有字母),没有参数。upper相反,将变量值转换为大写字母(所有字母),不带参数。cat连接多个变量。参数1:要连接的变量(String)。index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"Tobeornottobe");$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|cat:'这是一个问题'}输出:TobeornottobeTobeornottobeitisaquestioncount_characters计算变量内容中有多少个字符。参数1:计算总数时是否包含空格字符(Boolean,默认:false)。index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"ColdWaveLinkedtoTemperatures");$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|count_characters}{$articleTitle|count_characters:true}OUTPUT:ColdWaveLinkedtoTemperatures.2933count_paragraphs计算变量内容有多少段落。index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"我是个好人!\n\n是的,我是!");$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|count_paragraphs}输出:我是个好人!是的我是!2count_sentences统计变量内容有多少个句子。每个句子必须以句号、问号或感叹号结尾。(./?/!)index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"我是个好人。是的,我是!你确定吗?是的!");$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|count_sentences}输出:我是个好人。是的,我是!你确定吗?是的!4count_words用来统计有多少可变内容字。index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"我是个好人。是的,我是!你确定吗?是的!");$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|count_words}输出:我是个好人。我是!你确定吗?是的!12date_format以strftime()格式格式化日期和时间。时间可以是unix时间戳、DateTime对象、mysql时间戳或月-日-年格式的字符串,类似于PHP函数strtotime()。您可以完全控制date_format的格式。如果传给date_format的时间为空,给第二个参数传了一个值,那么第二个参数就会作为要格式化的时间。参数1:输出时间的格式定义(String,default:%b%e,%Y)参数2:如果输入为空,则作为默认时间(String,default:n/a)索引。php:$smarty=newSmarty;$config['date']='%I:%M%p';$config['time']='%H:%M:%S';$smarty->assign('config',$config);$smarty->assign('yesterday',strtotime('-1day'));$smarty->display('index.tpl');index.tpl:{$smarty.现在|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:Jan1,202201/01/2202:33pmDec31,2021Monday,December1,202114:33:00附件:date_format支持格式W3CDATE_FORMAT()函数default设置变量值的默认值。当变量为未设置或空字符串时,将显示默认值。必须有一个参数。参数一:变量为空时的输出值(String)->assign('email','');$smarty->display('index.tpl');index.tpl:{$articleTitle|默认:'无标题'}{$myTitle|默认:'无标题'}{$email|default:'Noemailaddressavailable'}OUTPUT:Iamagoodguy.YesIam!notitleNoemailaddressavailableescape可用于将变量编码或转换为html、url、单引号、十六进制、十六进制实体、javascript和电子邮件地址。默认为:html。参数一:这是转义后的格式(字符串,默认:html,可能值:html,htmlall,url,urlpathinfo,单引号,十六进制,十六进制实体,javascript,邮箱地址)参数二:传给htmlentities的字符集类型()(String,default:UTF-8,possiblevalues:ISO-8859-1,UTF-8,andothercharactersetssupportedbyhtmlentities())参数3:将entitytwice,&to&(仅用于html和htmlall)(布尔值,默认值:true)index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"'预计对无棺葬礼计划的强烈反对'");$smarty->assign('EmailAddress','smarty@example.com');$smarty->display('index.tpl');index.tpl&OUTPUT:{$articleTitle}'对无棺葬礼计划的强烈反对'{$articleTitle|escape}'预计对无棺葬礼计划的强烈反对'{$articleTitle|escape:'html'}'预计对无棺葬礼计划的强烈反对'{$articleTitle|escape:'htmlall'}tless葬礼计划'点击这里{$articleTitle|escape:'quotes'}\'预计对无棺葬礼计划的强烈反对\'{$EmailAddress|escape:"hexentity"}{$EmailAddress|escape:'mail'}{*这会将电子邮件转换为文本*bob..snip..et{'mail@example.com'|escape:'mail'}smarty[AT]example[DOT]com注意:unescape可以解码实体,html和htmlall等。对其进行编码与转义装饰器的效果完全相反。indent缩进字符串的每一行,默认是缩进4个空格。可选参数可以设置缩进的空格数。可选的第二个参数设置用于缩进的字符,例如“t”而不是用于缩进的空格。参数1:设置缩小进入多少空间(Integer,默认:4)参数2:设置用什么字符来进行缩小(String,默认:一个空间)index.php:$smarty=newSmarty;$smarty->assign'articleTitle','新泽西州法官将对裸体海滩作出裁决。预计今天有阳光或下雨,今晚则为黑暗。统计数据显示,25岁??后青少年怀孕率显着下降。'");$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|indent}{$articleTitle|indent:10}{$articleTitle|indent:1:"\t"}输出:新泽西州法官对裸体海滩作出裁决。预计今天有阳光或下雨,今晚天黑。统计数据显示,25岁??后青少年怀孕率显着下降。新泽西州法官将对裸体海滩作出裁决。今天预计会有阳光或下雨,今晚天黑。统计数据显示,25岁??后青少年怀孕率显着下降。新泽西州法官将对裸体海滩做出裁决.预计今天有太阳或下雨,今晚有天黑。统计数据显示,25岁??后青少年怀孕率显着下降。新泽西州法官将对裸体海滩作出裁决。预计太阳或下雨t天啊,今晚天黑了。统计数据显示,25岁以后少女怀孕率明显下降。nl2br将变量值中的所有“n”回车转成HTML
相当于PHP的nl2br()函数。index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"我是个好人。\n是的,我是!")$smarty->display('index.tpl');index.tpl:{$articleTitle|nl2br}OUTPUT:我是好人。
是的,我是!regex_replace用正则表达式搜索和替换变量值。使用PHP的preg_replace()函数。参数1:正则表达式(String,默认:n/a)参数2:要替换的字符(String,默认:n/a)index.php:$smarty=newSmarty;$smarty->assign('articleTitle',"我是个好人。\n是的,我是!")$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|regex_replace:"/[\r\t\n]/":""}输出:我是个好人。我是!我是个好人。我是!相当于PHP函数str_replace()。spacifyspacify将在变量的字符串中插入空格。您可以设置插入空格或其他字符。参数1:要在字符之间插入的字符(字符串,默认值:一个空格)index.php:$smarty=newSmarty;$smarty->assign('articleTitle','SomethingWentWronginJetCrash,ExpertsSay')$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|spacify}{$articleTitle|spacify:”^”}输出:喷气式飞机坠毁出了点问题,专家说。W....snip....sh,ExpertsSay.S^o^m^e^t^h^i^n^g^....snip....^e^r^t^s^^S^a^y^.string_format格式化字符串,如浮点数等。使用PHP函数sprintf()即可完成。参数1:指定格式(sprintf)(String,default:n/a)index.php:$smarty=newSmarty;$smarty->assign('number',3.1415926);$smarty->display('index.tpl');index.tpl:{$number}{$number|string_format:"%.2f"}{$number|string_format:"%d"}OUTPUT:3.14159263.143strip将连续的空格、回车和制表符转换为单个空格或指定的字符串。附言。如果你想转换模板文字中的空格,使用内置的{strip}函数。index.php:$smarty=newSmarty;$smarty->assign('articleTitle',“我很好,他们都喜欢我”);$smarty->display('index.tpl');index.tpl:{$articleTitle}{$articleTitle|strip}{$articleTitle|strip:''}输出:我太好了,他们都喜欢我我太好了,他们都喜欢我我太好了,他们都喜欢metruncate截取字符串到指定长度,默认长度为80。参数1:截取的长度(整数,默认:80)参数2:截取后代入显示的字符,计算该字符的长度为截取长度(String,default:...)参数3:默认truncate会尝试按word截取,如果要按字符截取(words可能会被截取),需要将第三个参数设置为TRUE。(Boolean,default:false)index.php:$smarty=newSmarty;$smarty->assign('articleTitle','两姐妹在收银台十八年后重逢');$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}输出:两姐妹收银台十八年后重逢收银台十八年后重逢收银台十八年后重逢双姐妹重逢...双姐妹重逢八年后重逢八后重逢双姐妹重逢E...双姐妹重逢..ckoutCounterwordwrap限制一行字符的长度(自动换行),效果同PHP函数wordwrap()。参数1:限制一行的长度(Integer,默认:80)参数2:换行符,可以自定义换行符(String,默认:n)参数3:设置为按字换行(FALSE),或换行符character(TRUE),默认情况下,换行符是基于单词的,即根据英文语法自动换行。如果你想按字符换行(边界处的单词会被拆分),你可以将可选的第三个参数设置为TRUEindex.php:$smarty=newSmarty;$smarty->assign('articleTitle','Blindwomangetsnewkidney来自她多年未见的爸爸');$smarty->display('index.tpl');index.tpl:$articleTitle}{$articleTitle|wordwrap:30}{$articleTitle|wordwrap:20}{$articleTitle|wordwrap:30:"
\n"}{$articleTitle|wordwrap:26:"\n":true}输出:盲人妇女从她多年未见的父亲那里得到新肾Blindwomangetsnewkidneyfrom多年不见的爸爸盲人妇女从多年未见的爸爸那里得到新肾盲人妇女从多年未见的爸爸那里得到新肾年3.复合装饰器可以组合使用多个装饰器。它们按照它们组成的顺序从左到右应用于变量。它们必须用“|”分隔(竖线)。