Smarty中常用变量操作符汇总

本文汇总了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@cn-web.com"} capitalize(首字母大写)

index.PHP页面如下:

代码如下:
assign('articleTitle','Police begin campaign to rundown jaywalkers.'); $smarty->display('index.tpl');

index.tpl页面如下:

代码如下:

OUTPUT输出如下:

代码如下:

count_characters(计算变量里的字符数)

index.PHP如下:

代码如下:
assign('articleTitle','Cold Wave Linked to Temperatures.'); $smarty->display('index.tpl');

index.tpl页面如下:

代码如下:
aracters}

OUTPUT输出如下:

Cold Wave Linked to Temperatures.

cat(连接字符串) 将cat里的值连接到给定的变量后面 index.PHP如下:

代码如下:
assign('articleTitle','Psychics predict world didn't end'); $smarty->display('index.tpl');

index.tpl页面如下:

代码如下:

OUTPUT输出如下:

代码如下:

count_paragraphs(计算段数) 计算变量里的段落数量 index.PHP如下:

代码如下:
assign('articleTitle','War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.'); $smarty->display('index.tpl');

index.tpl模板页面如下:

代码如下:
aragraphs}

OUTPUT输出如下:

代码如下:
ims 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如下:

代码如下:
assign('articleTitle','Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.'); $smarty->display('index.tpl');

index.tpl模板如下:

代码如下:

OUTPUT输出如下:

代码如下:
count_words(计算词数) 计算变量里的词数 index.PHP如下:
代码如下:
assign('articleTitle','Dealers Will Hear Car Talk at Noon.'); $smarty->display('index.tpl');

index.tpl模板如下:

代码如下:

OUTPUT输出如下:

代码如下:
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如下:

代码如下:
assign('yesterday',strtotime('-1 day')); $smarty->display('index.tpl');

index.tpl:

代码如下:
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,%Y"} {$yesterday|date_format:"%H:%M:%s"}

OUTPUT输出如下:

代码如下:
默认) Parameter Position Type required Default Description 1 string No empty This is the default value to output if the variable is empty.
这是变量为空的时候的输出 为空变量设置一个认值. 当变量为空或者未分配的时候,将由给定的认值替代输出. index.PHP如下:

代码如下:
assign('articleTitle','Dealers Will Hear Car Talk at Noon.'); $smarty->display('index.tpl');

index.tpl模板:

代码如下:

OUTPUT输出:

代码如下:
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如下:

<div class="codetitle"><a style="CURSOR: pointer" data="18741" class="copybut" id="copybut18741" onclick="doCopy('code18741')"> 代码如下:

<div class="codebody" id="code18741">$smarty = new Smarty;
$smarty->assign('articleTitle',"'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');

index.tpl模板:

<div class="codetitle"><a style="CURSOR: pointer" data="12996" class="copybut" id="copybut12996" onclick="doCopy('code12996')"> 代码如下:

<div class="codebody" id="code12996">{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} { escapes & " ' < > }
{$articleTitle|escape:"htmlall"} { escapes ALL html entities }
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a
href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}

OUTPUT输出:

<div class="codetitle"><a style="CURSOR: pointer" data="56790" class="copybut" id="copybut56790" onclick="doCopy('code56790')"> 代码如下:

<div class="codebody" id="code56790">'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'
<a
href="bob@me.netmailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net

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如下:

<div class="codetitle"><a style="CURSOR: pointer" data="98851" class="copybut" id="copybut98851" onclick="doCopy('code98851')"> 代码如下:

<div class="codebody" id="code98851">$smarty = new Smarty;
$smarty->assign('articleTitle','NJ judge to rule on nude beach.');
$smarty->display('index.tpl');

index.tpl模板:

<div class="codetitle"><a style="CURSOR: pointer" data="46108" class="copybut" id="copybut46108" onclick="doCopy('code46108')"> 代码如下:

<div class="codebody" id="code46108">{$articleTitle}

{$articleTitle|indent}

{$articleTitle|indent:10}

{$articleTitle|indent:1:"t"}

OUTPUT输出:

<div class="codetitle"><a style="CURSOR: pointer" data="57453" class="copybut" id="copybut57453" onclick="doCopy('code57453')"> 代码如下:

<div class="codebody" id="code57453">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如下:

代码如下:
assign('articleTitle','Two Convicts Evade Noose,Jury Hung.'); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:

nl2br(换行符替换成
) 所有的换行符将被替换成
.同PHP的nl2br()函数一样. index.PHP如下:

代码如下:
assign('articleTitle',"Sun or rain expectedntoday,dark tonight"); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:
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如下:

代码如下:
assign('articleTitle',"Infertility unlikely tonbe passed on,experts say."); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:

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如下:

代码如下:
assign('articleTitle',"Child's Stool Great for Use in Garden."); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:
spacify 是一种在字符串的每个字符之间插入空格或者插入其他的字符(串). index.PHP如下:
代码如下:
assign('articleTitle','Something Went Wrong in Jet Crash,Experts Say.'); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:

string_format(字符串格式化) Parameter Position Type required Default Description 1 string Yes n/a This is what format to use. (sprintf) 使用的格式化方式 是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化 index.PHP如下:

代码如下:
assign('number',23.5787446); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:

strip(去除(多余空格) 替换所有重复的空格,换行和tab为单个. index.PHP如下:

代码如下:
assign('articleTitle',"Grandmother ofneight makest hole in one."); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:

strip_tags(去除html标签) 去除在<和>之间的所有标签,包括<和>. index.PHP如下:

代码如下:
assign('articleTitle',"Blind Woman Gets from Dad she Hasn't Seen in years."); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:
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如下:
代码如下:
assign('articleTitle','Two Sisters Reunite after Eighteen Years at Checkout Counter.'); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
runcate} {$articleTitle|truncate:30} {$articleTitle|truncate:30:""} {$articleTitle|truncate:30:"---"} {$articleTitle|truncate:30:"":true} {$articleTitle|truncate:30:"...":true}
OUTPUT输出:
代码如下:

upper(大写 ) 将变量改为大写 index.PHP如下:

代码如下:
assign('articleTitle',"If Strike isn't Settled Quickly it may Last a While."); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
OUTPUT输出:
代码如下:

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如下:

代码如下:
assign('articleTitle',"Blind woman gets new kidney from dad she hasn't seen in years."); $smarty->display('index.tpl');
index.tpl模板:
代码如下:
n"} {$articleTitle|wordwrap:30:"n":true}
OUTPUT输出:
代码如下:
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页面如下:

代码如下:
assign('articleTitle','Smokers are Productive,but Death Cuts Efficiency.'); $smarty->display('index.tpl');

index.tpl模板:

代码如下:
runcate} {$articleTitle|lower|truncate:30|spacify} {$articleTitle|lower|spacify|truncate:30:". . ."}

OUTPUT输出:

代码如下:

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...