Smarty的简单指南.docx

上传人:b****7 文档编号:9090897 上传时间:2023-02-03 格式:DOCX 页数:11 大小:18.38KB
下载 相关 举报
Smarty的简单指南.docx_第1页
第1页 / 共11页
Smarty的简单指南.docx_第2页
第2页 / 共11页
Smarty的简单指南.docx_第3页
第3页 / 共11页
Smarty的简单指南.docx_第4页
第4页 / 共11页
Smarty的简单指南.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

Smarty的简单指南.docx

《Smarty的简单指南.docx》由会员分享,可在线阅读,更多相关《Smarty的简单指南.docx(11页珍藏版)》请在冰豆网上搜索。

Smarty的简单指南.docx

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="一二三四五六七八九"*}>

output:

一二三四五..

例2:

需求:

一组数据分两列显示,数据过长时需要换行。

两列要对齐,不能有空行。

这里需求什么,js实现了什么不重要,只要知道这里的数据的展现繁杂一点,需要js来参合。

tpl:

<{*datas=[xxxxxxx,xxxx,xxxx....]*}>

functioncreat3mulu(array){

varc=0;

for(vari=0,l=array.length;i

if(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;i

c++;

if(c<=banl){

left.push(array[i+1]);

}else{

right.push(array[i+1]);

}

if(array[i]>42){

c++;

}

}

left.push("

")

right.push("

")

returnleft.join("")+right.join("");

}

<{captureassign="json"}>

[<{foreachname=aforeachfrom=$datasitem=data}>

<$data|count_characters>,"

<{$data|strip|escape:

html}>

"

<{if$smarty.foreach.aforeach.last!

=true}>

<{/if}>

<{/foreach}>]

<{/capture}>

这里面比较有价值的代码是

<{captureassign="json"}>

[<{foreachname=aforeachfrom=$datasitem=data}>

<$data|count_characters>,"

<{$data|strip|escape:

html}>

"

<{if$smarty.foreach.aforeach.last!

=true}>

<{/if}>

<{/foreach}>]

<{/capture}>

这部分作用就是拼出一个js数组出来,拼出来的内容并不输出,而是储存在一个变量json中,以便后面的模板使用。

output:

[10,"

..
",50,"
..
",.....]

把这段代码稍加修改就是拼一个jsonobject出。

<{captureassign="json"}>

{<{foreachname=aforeachfrom=$datasitem=datakey=key}>

"<{$key|escape:

quotes}>":

"<{$data|strip|escape:

quotes}>"

<{if$smarty.foreach.aforeach.last!

=true}>

<{/if}>

<{/foreach}>}

<{/capture}>

output:

{"10":

"content0","50":

"content1",.....}

 

"

<{$data|strip|escape:

html}>

"

这部分模板作用是拼出一个js字符串。

可以想象的到如果data变量中有回车,双引号,那么肯定会报js错误。

变量调节器|strip|escape:

html就是去换行和字符实体转义(血淋淋的经验啊)

这里面$json就是前面拼好的json,不过里面含有大量的换行,$json|strip去换行。

我个人觉得为了效率,用js作为smarty模板的补充,用smarty来拼结构简单的json串很不错,也非常高效。

但是如果json格式比较复杂,拿模板来拼json一方面加大了工作量,一方面也增加了风险,提高了出现bug的概率。

所以,如果需要json非常复杂,还是要与rd商定接口,让rd来提供json。

展开阅读全文
相关搜索

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1