热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php模板引擎smarty的变量操作符

php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
语法中使用"|"应用变量操作符,多个参数用":"??指簟?/DIV>
capitalize[首字母大写]
count_characters[计算字符数]
cat[连接字符串]
count_paragraphs[计算段落数]
count_sentences[计算句数]
count_words[计算词数]
date_format[时间格式]
default[默认]
escape[转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成
]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
组合使用多个操作符
实例:
{* 标题大写 *}

{$title|upper}

{* 取其前40个字符 *}
Topic: {$topic|truncate:40:"..."}
{* 格式化文字串 *}
{"now"|date_format:"%Y/%m/%d"}
{* 在自定义函数里应用调节器 *}
{mailto|upper address="main@php118.com"}
capitalize(首字母大写)
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|capitalize}
 
OUTPUT:
 
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.

count_characters(计算变量里的字符数)
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|count_characters}
 
OUTPUT:
 
Cold Wave Linked to Temperatures.
32
cat(连接字符串)
将cat里的值连接到给定的变量后面
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle|cat:" yesterday."}
 
OUTPUT:
 
Psychics predict world didn't end yesterday.
count_paragraphs(计算段数)
计算变量里的段落数量
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|count_paragraphs}
 
OUTPUT:
 
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
 
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2
count_sentences(计算句数)
计算变量里句子的数量
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|count_sentences}
 
OUTPUT:
 
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2
count_words(计算词数)
计算变量里的词数
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|count_words}
 
OUTPUT:
 
Dealers Will Hear Car Talk at Noon.
7
date_format(日期格式)
Parameter Position
参数位置 Type Required Default Description
1 string No %b %e, %Y This is the format for the outputted date.
输出字串的格式 
2 string No n/a This is the default date if the input is empty.
输入为空时的默认设置
在给定的函数serftime();里格式日期和时间.
Unix或者mysql等的时间戳(parsable by strtotime)都可以传递到smarty.
设计者可以使用date_format完全控制日期格式.
如果传给date_format的数据是空的,将使用第二个参数作为时间格式
index.php:
 
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
 
index.tpl:
 
{$smarty.now|date_format}
{$smarty.now|date_format:"%A, %B %e, %Y"}
{$smarty.now|date_format:"%H:%M:%S"}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:"%H:%M:%S"}
 
OUTPUT:
 
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
default(默认)
Parameter Position Type Required Default Description
1 string No empty This is the default value to output if the variable is empty.
这是变量为空的时候的默认输出 
为空变量设置一个默认值.
当变量为空或者未分配的时候,将由给定的默认值替代输出.
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}
 
OUTPUT:
 
Dealers Will Hear Car Talk at Noon.
no title
escape(转码)
Parameter Position Type Required Possible Values Default Description
1 string No html,htmlall,url,quotes,hex,hexentity,Javascript html This is the escape format to use.
用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者Javascript转码.
默认是html转码
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');
 
index.tpl:
 
{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' <> *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}
 
OUTPUT:
 
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan'
'Stiff Opposition Expected to Casketless Funeral Plan'
indent(缩进)
Parameter Position Type Required Default Description
1 integer No 4 This determines how many characters to indent to.
2 string No (one space) This is the character used to indent with.
在每行缩进字符串,默认是4个字符(pear标准也是).
作为可选参数,你可以指定缩进字符数.
作为第二个可选参数,你可以指定缩进用什么字符代替
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$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.

lower(小写)
将变量字符串小写
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|lower}
OUTPUT:
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.

nl2br(换行符替换成
)
所有的换行符将被替换成
.同php的nl2br()函数一样.
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Sun or rain expectedntoday, dark tonight");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle|nl2br}
OUTPUT:
Sun or rain expected
today, dark tonight

regex_replace(正则替换)
寻找和替换正则表达式 .
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
替换正则表达式.
 
2 string Yes n/a This is the string of text to replace with.
使用什么文本字串来替换
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');
index.tpl:
{* replace each carriage return, tab & new line with a space *}{* 使用空格替换每个回车,tab,和换行符 *}
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}
OUTPUT:
Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.

replace(替换)
简单的搜索和替换字符串
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
将被替换的字符串
2 string Yes n/a This is the string of text to replace with.
用来替换的文本 
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT:
Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
spacify
是一种在字符串的每个字符之间插入空格或者插入其他的字符(串).
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 e n t W r o n g i n J e t C r a s h , E x p e r t s S a y .
S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^.

string_format(字符串格式化)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
使用的格式化方式
是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化
index.php:
$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
index.tpl:
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT:
23.5787446
23.58
24

strip(去除(多余空格)
替换所有重复的空格,换行和tab为单个.
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}
OUTPUT:
Grandmother of
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.

strip_tags(去除html标签)
去除在<和>之间的所有标签,包括<和>.
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|strip_tags}
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.
truncate(截取)
Parameter Position Type Required Default Description
1 integer No 80 This determines how many characters to truncate to.
指定截取多少字符
2 string No ... This is the text to append if truncation occurs.
截取后加在截取词后的字符串 
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).
检查是否截取到词的边界 
截取字符串开始的一段.默认是80个.
你可以指定第二个参数作为在截取的那段字符串后加上什么字符.
默认情况下,smarty会截取到一个词的末尾,
如果你想要精确的截取多少个字符,把第三个参数改为"true"
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}
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...

upper(大写 )
将变量改为大写
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|upper}
OUTPUT:
If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.

wordwrap(行宽约束)
可以指定段落的宽度(也就是多少个字符一行,超过这个字符数换行).默认80.
第二个参数可选,可以指定在约束点使用什么字符(默认是换行符n).
默认情况下smarty将截取到词尾,你也可以指定精确截取多少个字符
Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap to.
指 定段落(句子)的宽度 
2 string No n This is the string used to wrap words with.
使用什么字符约束 
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (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:30:"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 kidney fr
om dad she hasn't seen in year
s.
组合使用多个操作符
可以在一个变量上应用操作符,它们将从左到右依次组合应用.多个操作符必须用"|"符号分开.
index.php:
 
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');
$smarty->display('index.tpl');
 
index.tpl:
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}
 
 
OUTPUT:
 
Smokers are Productive, but Death Cuts Efficiency.
S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y .
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
s m o k e r s a r e p r o d u c t i v e , b u t . . .
s m o k e r s a r e p. . .

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 数据库(外键及其约束理解)(https:www.cnblogs.comchenxiaoheip6909318.html)My ... [详细]
  • yum安装_Redis —yum安装全过程
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Redis—yum安装全过程相关的知识,希望对你有一定的参考价值。访问https://redi ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
author-avatar
mobiledu2502936967
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有