- 浏览: 13731059 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
-
php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
语法中使用"|"应用变量操作符,多个参数用":"??指簟?/DIV>
capitalize[首字母大写]
count_characters[计算字符数]
cat[连接字符串]
count_paragraphs[计算段落数]
count_sentences[计算句数]
count_words[计算词数]
date_format[时间格式]
default[默认]
escape[转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成<br />]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
组合使用多个操作符
实例:
{* 标题大写 *}
<h2>{$title|upper}</h2>{* 取其前40个字符 *}
Topic: {$topic|truncate:40:"..."}{* 格式化文字串 *}
{"now"|date_format:"%Y/%m/%d"}{* 在自定义函数里应用调节器 *}
{mailto|upper address="main@cn-web.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.
2count_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:00default(默认)
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 titleescape(转码)
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"}
<a
href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
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'
<a
href="bob@me.netmailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net</a>
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(换行符替换成<br /> )
所有的换行符将被替换成 <br />.同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<br />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 <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|strip_tags}
OUTPUT:
Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.
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:"<br>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<br>
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. . .
发表评论
-
smarty中英文多编码字符截取乱码问题
2010-01-11 21:52 2335一般网站页面的显示都不可避免的会涉及子字符串的截取,这个时候t ... -
smarty中js的调用方法
2010-01-11 21:51 6464有时候,在smarty中,包含js的时候,整个页面就不会显示, ... -
FCKeditor + smarty
2010-01-11 21:49 1904FCKeditor是目前互联网上最好的在线编辑器。 smar ... -
smarty 简单分页
2010-01-11 21:46 2083以下是模板中的smarty代码,用smarty简单的代入相关的 ... -
smarty半小时快速上手教程
2010-01-11 21:45 2441smarty的程序设计部分: 在smarty的模板 ...
相关推荐
(跟我PHP中常用的PHP内部函数类似)2、如何使用Smarty变量操作符语法中使用"|"应用变量操作符,多个参数用":" 分隔开来3、介绍常用的20个变量符capitalize [首字母大写] count_characters [计算字符数] cat [连接...
软件介绍 1、什么是Smarty变量操作符? php模板引擎smarty内置的一些操作函数,我们称之为变量操作符,变量操作符 可用于操作变量,自定义函数和字符。(跟我PHP中常用的PHP内部函数类似) 2、如何使用Smarty变量...
接下来,我们将探讨20个常用的Smarty变量操作符: 1. {$var}:这是最基本的变量输出,直接显示变量的值。 2. {$var|escape:"html"}:用于转义HTML特殊字符,防止XSS攻击。 3. {$var|upper} 和 {$var|lower}:分别将...
【标题】"PHP100视频教程28:PHP模板引擎Smarty的变量操作符.rar" 涉及的核心知识点是PHP中的Smarty模板引擎及其变量操作符。Smarty是一款强大的PHP模板技术,它将业务逻辑与视图层分离,使得前端开发者可以更专注于...
以下是一些常用的Smarty变量操作符及其功能: 1. capitalize:使变量的首字母大写。例如,如果变量值为"policebegincampaigntorundownjaywalkers",使用此操作符后将变为"Policebegincampaigntorundownjaywalkers...
本文将详细介绍PHP Smarty模板引擎中常用的20个变量操作符及其使用方法。 1. **capitalize** - 首字母大写:此操作符将字符串的第一个字符转换为大写,其他字符保持不变。如`{$title|capitalize}`。 2. **count_...
变量调节器能够对模板中的变量进行格式化、过滤、排序等操作,非常方便。但是,在实际开发过程中,可能会遇到变量调节器失效的情况,这往往让开发者感到非常头疼。一旦碰到这种情况,首先应该检查的就是变量后是否...
Smarty是一个流行的PHP模板引擎,它的设计目标是将HTML代码与PHP代码分离,使得...它不仅介绍了Smarty的基本操作,还涉及到了实际应用中的高级特性,帮助开发者更好地利用Smarty来构建高效、易维护的PHP应用程序。
### Smarty变量修饰基础知识 变量修饰是Smarty模板语言中的一个重要组成部分。它通过在变量后使用管道符号“|”添加修饰符来实现。修饰符可以对变量进行各种操作,如格式化、过滤、转义等。SMARTY自带了一些内置...
Smarty提供了丰富的变量修饰符,如`|escape:`用于转义HTML特殊字符,`|truncate:`用于截断字符串,`|date_format:`用于格式化日期等。这些修饰符可以在变量输出前进行处理,增强模板的表达能力。 5. 动态块函数 ...
3. **模版解析**:解析模版文件,将变量值插入到相应的占位符位置。 4. **输出结果**:将解析后的HTML代码输出到浏览器。 #### 五、Smarty的特点与优势 1. **易于使用**:尽管初学者可能会觉得Smarty有些难以理解...
修饰器可以对变量进行格式化、过滤、转换等操作,扩展了模板的功能。 1. 变量 在Smarty模板中,变量可以是标量值(如数字或字符串),也可以是数组或对象。使用数组时,可以采用数字索引或关联字符串索引。例如: `...
在Smarty中,运算符主要用于模板文件中对变量进行操作。接下来,我们将对Smarty中常用的运算符进行详细介绍。 ##### 比较运算符 1. **等于(`{eq}`)**:用于判断两个值是否相等。 ```smarty {$var1|default:""|...
2. 控制结构: Smarty支持if/else、foreach等控制结构,使得模板中可以进行条件判断和循环操作。 3. 函数和修饰符:Smarty提供了一系列内置函数和修饰符,用于处理模板中的数据,如`{date format="Y-m-d H:i:s"}`...
- **变量操作**:提供多种变量操作,如数组处理、条件判断、循环等。 3. **Smarty的安装与配置**: 首先需要下载Smarty库,然后在PHP项目中进行适当的配置,包括设置模板目录、编译目录、缓存目录等。通过require...
2. **变量和函数**:在Smarty中,可以定义和传递变量给模板,模板文件可以通过内置的函数和操作符对这些变量进行处理,如格式化日期、字符串操作等。 3. **缓存机制**:Smarty提供了强大的缓存功能,可以缓存编译后...
5. **函数和插件**:Smarty拥有丰富的内置函数和可扩展的插件系统,可以对模板进行各种操作,如字符串格式化、日期处理等。 6. **安全控制**:为了防止XSS(跨站脚本攻击),Smarty提供了变量过滤和安全模式,确保...
- 可以对变量应用修饰符,如 `{ $Name|upper }` 表示将 `$Name` 变量中的内容全部转为大写。 **2. 注释** - **注释语法**: - 在模板文件中使用 `{* *}` 来添加注释。 **3. 文件引入** - **includefile 函数**...
- 对象属性的 " ." 操作符:`{$foo.a.b.c}` 此外,Smarty3 还支持在变量名中使用变量,甚至表达式,以及对象链式调用,使得模板语言更为强大和灵活。 总的来说,Smarty3 提供了更现代的语法和更好的性能,同时保持...
5. **插件系统**:Smarty允许开发自定义函数(函数插件)和变量修饰符(修饰符插件),增强了模板的灵活性。例如,可以创建一个日期格式化插件,将时间戳转化为易读的日期。 6. **配置文件**:Smarty使用`.conf`...