加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

Smarty中常用变量操作符汇总

发布时间:2020-05-25 17:04:28 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了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页面如下:

代码如下:

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模板页面如下:

代码如下:

OUTPUT输出如下:

代码如下:

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

代码如下:

display('index.tpl');

index.tpl模板如下:

代码如下:

OUTPUT输出如下:

代码如下:

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

index.tpl:

代码如下:

OUTPUT输出如下:

代码如下:

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

index.tpl模板:

代码如下:

OUTPUT输出:

代码如下:

<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.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读