Smarty的简单指南.docx
《Smarty的简单指南.docx》由会员分享,可在线阅读,更多相关《Smarty的简单指南.docx(11页珍藏版)》请在冰豆网上搜索。
![Smarty的简单指南.docx](https://file1.bdocx.com/fileroot1/2023-2/2/a5cb6940-79d1-4696-9ab1-71619e781823/a5cb6940-79d1-4696-9ab1-71619e7818231.gif)
Smarty的简单指南
Smarty是什么,能做什么
Smarty是一个php模板引擎。
更准确的说,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法。
如果把开发者分为两种2种角色:
programmeranddesigner(翻译成程序员和美工似乎贴切一点),那么Smarty就是为美工(designer)设计的。
这个两种角色基本上不可能是一个人兼任,具体到我们的工作中,ui+fe基本等于designer,rd=programmer。
所以我们fe来接受模板是最符合smarty的设计初衷,是最合适不过。
最理想的情况下,如果有一天rd想要改变文章检索的方式(也就是程序逻辑的改变)。
这个改变不影响模板,内容仍将准确的输出到模板。
同样的,如果哪天只是要完全重做界面,也不会影响到程序逻辑。
因此,rd可以改变逻辑而不需要重新构建模板,我们可以改变模板而不影响到逻辑。
这里只是介绍对我们模版设计者有用的地方,至于是否作缓存等服务器端的问题,我们不关心。
Smarty安装
Smarty安装非常简单,从
第一个Smarty
这里是最最最简单的smarty例子,把index.php置于yourwebfolder下,把Index.tpl置于yourwebfoldertemplates下。
模版目录名字可以自己设定。
Index.php
php
include_once("./comm/Smarty.class.php");//包含smarty类文件
$smarty=newSmarty();//建立smarty实例对象$smarty
$smarty->template_dir="./templates";//设置模板目录
$smarty->compile_dir="./templates_c";//设置编译目录
$smarty->left_delimiter="<{";//设置smarty标签左右的标志符号
$smarty->right_delimiter="}>";
$smarty->assign("name","ctl");//进行模板变量替换
$smarty->assign("str","hellosmarty!
");//
$smarty->display("index.tpl");//编译并显示位于./templates下的index.tpl模板
?
>
Index.tpl
Inserttitlehere<{$name}>,<{$str}>
output:
ctl,hellosmarty!
(默认的语法标签标志符号是’{’和’}’,与js冲突,所以一般都换成’<{’和’}>’,以下所以例子都是使用自定义的标志符号)
实际开发中php不用我们操心,这部分是由rd负责。
我们关心的是tpl,和数据变量(如$name,$str)。
虽然在smarty模版(tpl)中可以嵌入php代码,但是这种做法把本来分离开的逻辑和展现又混淆了,所以是强烈不推荐的。
另外在模版里头嵌入php代码会引起rd的不安(就如同rd写了一段js,嵌入页面中会引起咱们的不安一样)。
Smarty的语法标签
smary中有很多的标签,大多数只适合某些场景,这里列出最常用到的几种
输出变量内容
tpl:
<{*str=helloworld!
这是smarty的注释方式*}>
<{$str}>
output:
helloworld!
注意:
<{…}>中间不能使用php的函数。
变量调节器
就是字面意思,把变量输出的值按照一定逻辑进行变换。
Smarty提供了很多变量调节器,不过都针对于英文。
count_characters
字符长度。
汉字在gbk方式下长度是2,uft-8方式下长度是3。
tpl:
<{*str=helloworld!
*}>
<{$str|count_characters}>
<{$str|count_characters:
true}>//参数true代表是否计算空格
output:
12
11
escape
转义。
tpl:
<{*str=‘hello&world’*}>
<{$str|escape:
”html”}>//把&"'<>变成字符实体
output:
'hello&world'
regex_replace
正则替换
tpl:
<{*str=helloworld!
*}>
<{$str|regex_replace:
”/world/”:
”smarty”}>
output:
hellosmarty!
strip
用一个空格或一个给定字符替换所有重复空格,换行和制表符.
tpl:
<{*str=hello\nworld!
*}>
<{$str|strip}>
output:
helloworld!
组合变量调节器
所有的变量调节器都可以组合使用
tpl:
<{*str=hello&\nworld!
*}>
<{$str|strip|escape:
”html”}>
output:
Hello&world!
条件if,elseif,else
可以使用以下条件修饰词:
eq、ne、neq、gt、lt、lte、le、gte、ge、iseven、isodd、isnoteven、isnotodd、not、mod、divby、evenby、oddby、==、!
=、>、<、<=、>=.使用这些修饰词时必须和变量或常量用空格格开。
逻辑符号同js一致
tpl:
<{if$nameeq"world"}>
Hello,world!
.
<{elseif$nameeq"smarty"}>
Hello,smarty!
.
<{else}>
Hello!
<{/if}>
循环foreach,foreachelse
循环简单数组
tpl:
<{*example=[“item0”,”item1”]*}>
<{foreachfrom=$exampleitem=theitemname=thename}>
index:
<{$smarty.foreach.thename.iteration}>
isfirst:
<{$smarty.foreach.thename.first}>
islast:
<{$smarty.foreach.thename.last}>
<{$theitem}>
<{/foreach}>
Output:
iteration:
1
isfirst:
true
islast:
false
item0
iteration:
2
isfirst:
false
islast:
true
item1
cycle
用于轮转一组数值。
例如常见的表格隔行变色
tpl:
<{*datas=[1,2,3]*}>
<{foreachfrom=$datasitem=data}>
">
<{$data}> | <{/foreach}>
OUTPUT:
1 |
2 |
3 | capture
capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面.tpl:
<{captureassign="avar"}>
<{*$array=['i1','i2','i3']*}>
<{foreachfrom=$arrayitem=theitem}>
<{$item}>
<{/foreach}>
<{/capture}>
avarvalue:
<{$avar|strip}>
output:
avarvalue:
i1i2i3
eval
功能类似于js中的eval,就是把变量内容当作模版来解析。
tpl:
<{*str='helloworld!
'code='<{$str}>'*}>
<{evalvar=code}>
output:
helloworld!
smarty与js
smarty不错,灵活应用它所提供的语法标签能够实现绝大部分需求。
不过,有的需求用smarty模板来做的确复杂低效,或者干脆就没办法做到。
这个时候,就靠js了。
简单说就是把模版中的数据拼成json来用js处理。
例1:
smarty的变量调节器提供的截断功能不支持中文。
下面是一个截断的例子。
tpl:
<{*str="一二三四五六七八九"*}>
functiontruncation(str){
for(varj=0,c=0;jc++;
if(str.charCodeAt(j)>128){
c++;
}
}
returnstr.substr(0,j)+(str.length>j?
"..":
"");
}
document.write(truncation("<{$str|strip|escape:
html}>")));
output:
一二三四五..
例2:
需求:
一组数据分两列显示,数据过长时需要换行。
两列要对齐,不能有空行。
这里需求什么,js实现了什么不重要,只要知道这里的数据的展现繁杂一点,需要js来参合。
tpl:
<{*datas=[xxxxxxx,xxxx,xxxx....]*}>
functioncreat3mulu(array){
varc=0;
for(vari=0,l=array.length;iif(array[i]>42){
c++;
}
c++;
}
varbanl=Math.round(c/2);
varleft=[];
varright=[];
left.push("left'>");
right.push("left'>");
c=0;
for(vari=0,l=array.length;ic++;
if(c<=banl){
left.push(array[i+1]);
}else{
right.push(array[i+1]);
}
if(array[i]>42){
c++;
}
}
left.push("