资讯专栏INFORMATION COLUMN

Smarty(变量修饰器)

linkin / 1198人阅读

摘要:一概念变量修饰器调节器可用于变量,自定义函数和字符串。请使用符号和修饰器名称应用修饰器。变量修饰器由赋予的参数值决定其行为。与之相反将变量值转成大写字母所有字母,无参数。它与修饰器的效果刚好相反。它们会按复合的顺序来作用于变量,从左到右。

一、概念

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

二、修饰器用法简介 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!


                                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'

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

{"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:"	"}

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.
Yes 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.
Yes I am!")
$smarty->display("index.tpl");

index.tpl:

{$articleTitle}
{$articleTitle|regex_replace:"/[
	
]/":" "}

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
good that	    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:"
"} {$articleTitle|wordwrap:26:" ":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
三、复合修饰器

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/26105.html

相关文章

  • Smarty(一)

    摘要:还有安全特性,可以令模板源码有强制的约束。并且业务逻辑和显示逻辑分离,是的一个设计理念。是一个模板引擎,在应用程序中,当作层视图层的组件来使用。不一定保证良好的程序设计或者达成显示分离的目的,这还得需要开发者和模板设计师的努力。 一、 Smarty的由来 Smarty的主要设计理念是:干净的分离显示逻辑和程序代码,在PHP为后端情况下,Smarty模板应用在前端,增强PHP的前后端交互...

    aboutU 评论0 收藏0
  • Smarty(一)

    摘要:还有安全特性,可以令模板源码有强制的约束。并且业务逻辑和显示逻辑分离,是的一个设计理念。是一个模板引擎,在应用程序中,当作层视图层的组件来使用。不一定保证良好的程序设计或者达成显示分离的目的,这还得需要开发者和模板设计师的努力。 一、 Smarty的由来 Smarty的主要设计理念是:干净的分离显示逻辑和程序代码,在PHP为后端情况下,Smarty模板应用在前端,增强PHP的前后端交互...

    chenjiang3 评论0 收藏0
  • 从零开始打造自己的PHP框架——第4章

    摘要:本篇,我们就在自己的项目中使用来进行依赖管理。输入,即可安装依赖到目录中,并且自动生成和。安装依赖,提示从提示可以看出,读取的是中的依赖,而不是最新的。访问,正常工作。源码分享书签从零开始打造自己的框架中文网中文文档 Composer Composer 是 PHP5.3以上 的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。Composer 不是一个包管...

    madthumb 评论0 收藏0
  • PHP_Smarty

    摘要:体现了业务与显示的分离,尽量分离。就负责判断条件,并取出数据来。显示的工作尽量靠前页面缓存缓存,重要概念。减轻了数据库的压力。控制局部不缓存在标签中控制,该标签不缓存。模板调用特殊方法使用对象注册的方式来解决。文件命名函数名定 模板 数据与表现层的标签分离 smarty是PHP 与 HTML代码的分离 小型模板类 $smarty 的工作流程: 把需要显示的全局变量,赋值塞到对象内部的...

    printempw 评论0 收藏0
  • Smarty 模板函数

    摘要:最近工作中用到模板引擎,整理了一些用到的模板函数。模板中获取服务端的值参数名模板中获取服务端的值参数名模板中获取服务端的值可以获取或的值。 最近工作中用到 Smarty 模板引擎,整理了一些用到的模板函数。 假设 smarty 的定界符为 {}。 模板中获取服务端 $_GET 的值: {$smarty.get.参数名} 模板中获取服务端 $_POST 的值: {$smarty.post...

    zhiwei 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<