Heim  >  Artikel  >  Backend-Entwicklung  >  Smarty模板变量操作符总结_PHP教程

Smarty模板变量操作符总结_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:31:10967Durchsuche

很久没有使用Smarty做项目了,今天突然要改改自己的网站里面的一些东西,因为我网站是很久以前用smarty模板引擎做的,改着改着有些地方就感觉还得网上搜答案了,看来对久违的smarty已经开始生疏了,赶紧补习一下。对Smarty模板的变量操作符稍微做一下总结,以备后用。

Smarty模板变量操作符介绍

capitalize [首字母大写],示例:{$smarty.get.name|capitalize}

count_characters [计算字符数],示例:{$smarty.get.name|count_characters}

cat [连接字符串],示例:{$smarty.get.name|cat:'welcome to www.phpernote.com'},连接两个变量如:{$smarty.get.name|cat:$smarty.get.password}

count_paragraphs [计算段落数],示例:{$smarty.get.name|count_characters}
 
count_sentences [计算句数],示例:{$smarty.get.name|count_sentences}

count_words [计算词数],示例:{$smarty.get.name|count_words}

date_format [时间格式],示例:{$smarty.server.request_time|date_format:"%Y-%m-%d %H:%M:%S"}或者{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"},显示格式如:2012-7-31 11:01:21

default [默认],示例:{$smarty.get.name|default:'phpernote.com'},意思是当没有接受到name参数或者接收到的name参数值为空的时候,使用phpernote.com代替这个值。
 
escape [转码],示例:{$smarty.get.name|escape:'url'} ?

indent [缩进],示例:{$smarty.get.name|indent:10:' '},段落首行缩进,这里示例是缩进了十个空格表示的字符。作用类似于css里面的text-indent。

lower [小写],示例:{$smarty.get.name|lower}

upper [大写],示例:{$smarty.get.name|upper}

nl2br [将变量中的\n(换行符)替换成
],示例:{$smarty.get.name|nl2br}

regex_replace [正则替换],示例:{$smarty.get.name|replace:"/[\r\t\n]/":"phpernote.com"},将变量中的符合正则的内容替换成指定内容

replace [替换],示例:{$smarty.get.name|replace:"baidu.com":"phpernote.com"},将变量中要求的内容替换成指定内容,示例的意思是将变量中所有baidu.com替换为phpernote.com。

spacify [插空],示例:{$smarty.get.name|spacify:"^^"},将变量字符与字符之间插入指定内容,包括空格。

string_format [字符串格式化],示例:{$smarty.get.namestring_format:"%d"},该操作是将变量中的内容格式化,格式化参数类同php的printf。

strip [去除(多余空格)],示例:{$smarty.get.name|strip:" "},如果存在第一个参数的话,则将空格替换为指定的内容,示例的意思是将空格都替换为" ";
 
strip_tags [去除html标签],示例:{$smarty.get.name|strip_tags}

truncate [截取],示例:{$smarty.get.name|truncate:10:'...'},示例的意思是截取10个字符,超出的部分用"..."代替,第二个参数为空的话,超出部分就直接省略了。

wordwrap [行宽约束],示例:{$smarty.get.name|wordwrap:30:"\n":true},将超过指定的长度的内容强制换行

今天就列举到此了,以后会慢慢补充和完善,更为具体的Smarty模板变量操作符知识大家可以参考Smarty手册!

您可能感兴趣的文章

  • smarty模板中使用php函数以及smarty模板中如何对一个变量使用多个函数
  • smarty变量操作符总结
  • smarty模板保留变量总结
  • Thinkphp 模板中常用的系统变量总结
  • smarty模板中如何生成随机数
  • 对用户传入的变量进行转义操作处理,摘自ecshop
  • 程序中通过define定义的常量如何在smarty模板中使用
  • smarty include file 使用变量的方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/764136.htmlTechArticle很久没有使用Smarty做项目了,今天突然要改改自己的网站里面的一些东西,因为我网站是很久以前用smarty模板引擎做的,改着改着有些地方...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn