一、什么是smarty?
smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分
离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目
中显的尤为重要。
二、smarty优点:
1. 速度:采用smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。
2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的方式,在下一次访
问模板时将WEB请求直接转换到这个文件中,而不再进行模板重新编译(在源程序没有改动的情况下)
3. 缓存技术:smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定smarty的cache属性为
true时,在smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来,这相当于调用一个静态的HTML文件。
4. 插件技术:smarty可以自定义插件。插件实际就是一些自定义的函数。
5. 模板中可以使用if/elseif/else/endif。在模板文件使用判断语句可以非常方便的对模板进行格式重排。
三、不适合使用smarty的地方:
1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新,这类型的程序使用smarty会使模板处理速度变慢。
2. 小项目。小项目因为项目简单而美工与程序员兼于一人的项目,使用smarty会丧失php开发迅速的优点。
四、安装smarty类:
安装smarty的环境:php版本4.06以上版本。
安装smarty方法非常简单,从 http://samrty.php.net中下载smarty.t...将LIB中所有文件
拷入comm目录,完成基本安装.
其它高级安装使用方法请看手册.
五、smarty在模板中的使用:
本节通过几个实例来讲一讲smarty的使用。smarty模板通常使用.tpl来标识,有些人为了美工方便,将扩展名直接写成.html,也是可以
的。本文中采用smarty标准写法:以.tpl来表示为一个smarty模板。
>
> PHP代码:
>
> * * *
>
>
>
>
> > 实例1:
>
> >
>
> > 先来看一个简单的例子。
>
> > =====================================================
>
> > index.tpl
>
> > =====================================================
>
> >
>
> > {* 显示是smarty变量识符里的用*包含的文字为注释内容 *}
>
> > {include file="header.tpl"}{页面头}
>
> > 大家好,我叫{ $name}, 欢迎大家阅读我的smarty学习材料。
>
> > {include file="foot.tpl"}{页面尾}
>
> >
>
> > 上边的这个例子是一个tpl模板,其中:
>
> > 1. {**}是模板页的注释,它在smarty对模板进行解析时不进行任何输出,仅供模板设计师对模板进行注释。
>
> > 2. {include file="xxx.tpl"}使用此句将一个模板文件包含到当前页面中,例子中将在网站中公用事的head.tpl与foot.tpl进行了包含,你可以
>
> > 这样想,使用这一句将xxx.tpl中的内容全部复制在当前语句处。当然,你不使用这一句也可以,将XXX.tpl中的内容复制到当前语句处
>
> > 也是完全可以了。
>
> >
>
> > 3.{ $name}: 模板变量,smarty中的核心组成,采用smarty定义的左边界符{与右边界符}包含着、以PHP变量形式给出,在smarty程序中将使用
>
> > $smarty->assign("name", "李晓军");将模板中的 $name替换成“李晓军”三个字。
>
> >
>
> > 整个实例源程序如下:
>
> > =============================
>
> > header.tpl
>
> > =============================
>
> >
1<html>
2>
3> > <head>
4>
5> > <title>大师兄smarty教程</title>
6>
7> > </head>
8>
9> > <body>
10>
11> >
12>
13> >
14>
15> > ===============================
16>
17> > foot.tpl
18>
19> > ===============================
20>
21> > <hr/>
22>
23> > <center> CopyRight(C) by 大师兄 2004年8月 Email: [email protected] </center>
24>
25> > <hr/>
26>
27> > </body>
28>
29> > </html>
>
> >
>
> > =====================================================
>
> > index.tpl
>
> > =====================================================
>
> >
>
> > {* 显示是smarty变量识符里的用*包含的文字为注释内容 *}
>
> > {include file="header.tpl"}{页面头}
>
> > 大家好,我叫{ $name}, 欢迎大家阅读我的smarty学习材料。
>
> > {include file="foot.tpl"}{页面尾}
>
> >
>
> > ================================================
>
> > index.php
>
> > ================================================
>
> > ```
> /********************************************* > * > * 文件名: index.php > * 作 用: 显示实例程序 > * > * 作 者: 大师兄 > * Email: [email protected] > * > *********************************************/ > include_once("./comm/Smarty.class.php"); //包含smarty类文件 > > $smarty = new Smarty(); //建立smarty实例对象 $smarty > $smarty->templates("./templates"); //设置模板目录 > $smarty->templates_c("./templates_c"); //设置编译目录 > > //---------------------------------------------------- > //左右边界符,默认为{},但实际应用当中容易与JavaScript > //相冲突,所以建议设成<{}>或其它。 > //---------------------------------------------------- > $smarty->left_delimiter = "{"; > $smarty->right_delimiter = "}"; > > $smarty->assign("name", "李晓军"); //进行模板变量替换 > > //编译并显示位于./templates下的index.tpl模板 > $smarty->display("index.tpl"); >
1>
2> >
3>
4> > 最终执行这个程序时将显示为:
5>
6> > ================================
7>
8> > 执行index.php
9>
10> > ================================
11>
12> >
> > >
> > > > > > ``` > > > > > > > > > 实例2: > > > 这个例子是综合使用smarty模板参数的一个例子,这些参数用来控制模板的输出,我只选其中几个,其它的参数你去看参考吧。 > > > > > > ================================================ > > > exmple2.tpl > > > ================================================ > > > ```> > >
> > > 2. 第二句模板变量 + 李晓军:{ $str2|cat:"李晓军"}
> > > 3. 第三句输出当前日期:{ $str3|date_format:"%Y年%m月%d日"} > > > 4. 第四句.php程序中不处理,它显示默认值:{ $str4|default:"没有值!"} > > > 5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:
> > > { $str5|indent:8:"*"}}
> > > 6. 第六句把[email protected]全部变为小写:{ $str6|lower}
> > > 7. 第七句把变量中的teacherli替换成:李晓军:{ $str7|replace:"teacherli":"李晓军"}
> > > 8. 第八句为组合使用变量修改器:{ $str8|capitalize|cat:"这里是新加的时间:"|date_format:"%Y年%m月%d日"|lower} > > > > > > ``` > > > > > > =============================================== > > > example2 .php > > > =============================================== > > > ```
> /********************************************* > * > * 文件名: example2.php > * 作 用: 显示实例程序2 > * > * 作 者: 大师兄 > * Email: [email protected] > * > *********************************************/ > include_once("./Smarty.class.php"); //包含smarty类文件 > > $smarty = new Smarty(); //建立smarty实例对象 $smarty > $smarty->templates("./templates"); //设置模板目录 > $smarty->templates_c("./templates_c"); //设置编译目录 > > //---------------------------------------------------- > //左右边界符,默认为{},但实际应用当中容易与JavaScript > //相冲突,所以建议设成<{}>或其它。 > //---------------------------------------------------- > $smarty->left_delimiter = "{"; > $smarty->right_delimiter = "}"; > > $smarty->assign("str1", "my name is xiao jun, li."); //将str1替换成My Name Is Xiao Jun, Li. > $smarty->assign("str2", "我的名字叫:"); //输出: 我的名字叫:李晓军 > $smarty->assign("str3", "公元"); //输出公元2004年8月21日(我的当前时间) > // $smarty->assign("str4", ""); //第四句不处理时会显示默认值,如果使用前面这一句则替换为"" > $smarty->assign("str5", "前边8个*"); //第五句输出:********前边8个* > $smarty->assign("str6", "[email protected]"); //这里将输出[email protected] > $smarty->assign("str7", "this is teacherli"); //在模板中显示为:this is 李晓军 > $smarty->assign("str8", "HERE IS COMBINING:"); > > //编译并显示位于./templates下的index.tpl模板 > $smarty->display("example2.tpl"); >
1>
2> >
3>
4> > 最终输出效果:
5>
6> > ======================================================
7>
8> > example2.php输出效果:
9>
10> > ======================================================
11>
12> >
> > > 2. 第二句模板变量 + 李晓军:我的名字叫:李晓军
> > > 3. 第三句输出当前日期:公元2004年8月21日
> > > 4. 第四句.php程序中不处理,它显示默认值:没有值!
> > > 5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:
> > > ********前边8个*
> > > 6. 第六句把[email protected]全部变为小写:[email protected]
> > > 7. 第七句把变量中的teacherli替换成:李晓军:this is 李晓军
> > > 8. 第八句为组合使用变量修改器:Here is Combining:这里是新加的时间:2004年8月21日 > > > > > > ``` > > > > > > 在模板中的这些参数被称为变量修改器(variable modifiers),使用这些参数可对模板进行一系列的修改控制。变量修改器 > > > 使用"|"和调节器名称应用修改器, 使用":"分开修改器参数。变量修改器可以组合使用,像第八句一样,实际使用中可以灵活应用。 > > > > > > > > > > > > 实例3. > > > ================================================== > > > example3.tpl > > > ================================================== > > > ```> > >
> > > {html_checkboxes name="CheckBox" values= $CheckName checked= $IsChecked output= $value separator="
"} > > > 下面在这一行将显示3个radio:
> > > {html_radioes name="RadioBox" values= $RadioName checked= $IsChecked output= $value separator="
"} > > > > > > > > > 下面显示一个月,日, 年选择框:
> > > {html_select_date} > > > > > >
CopyRight(C) By XiaoJun, Li 2004{mailto address="[email protected]" text="联系作者"} > > > > > > > > > ``` > > > > > > ====================================================== > > > example3.php > > > ====================================================== > > > ```
> /********************************************* > * > * 文件名: example3.php > * 作 用: 显示实例程序3 > * > * 作 者: 大师兄 > * Email: [email protected] > * > *********************************************/ > > require_once ("./comm/Smarty.class.php"); > > $smarty = new F117_Smarty; > $smarty->template_dir = './templates/'; > $smarty->compile_dir = './templates_c/'; > $smarty->config_dir = './configs/'; > $smarty->cache_dir = './cache/'; > $smarty->caching = false; > > //-------------------------------------------------------------------------------------- > //处理{html_checkboxes name="CheckBox" values= $CheckName checked= $IsChecked output= $value separator="
1<br>"}
2&gt;
3&gt; &gt; //--------------------------------------------------------------------------------------
4&gt;
5&gt; &gt; $smarty-&gt;assign('CheckName', array(
6&gt;
7&gt; &gt; 1001 =&gt; '语文',
8&gt;
9&gt; &gt; 1002 =&gt; '数学',
10&gt;
11&gt; &gt; 1003 =&gt; '外语'));
12&gt;
13&gt; &gt; $smarty-&gt;assign('IsChecked', 1001);
14&gt;
15&gt; &gt;
16&gt;
17&gt; &gt;
18&gt;
19&gt; &gt; //--------------------------------------------------------------------------------------
20&gt;
21&gt; &gt; //处理{html_radioes name="RadioBox" values= $RadioName checked= $IsChecked output= $value separator="<br>"}
22&gt;
23&gt; &gt; //--------------------------------------------------------------------------------------
24&gt;
25&gt; &gt; $smarty-&gt;assign('RadioName', array(
26&gt;
27&gt; &gt; 1001 =&gt; '语文',
28&gt;
29&gt; &gt; 1002 =&gt; '数学',
30&gt;
31&gt; &gt; 1003 =&gt; '外语'));
32&gt;
33&gt; &gt; $smarty-&gt;assign('IsChecked', 1001);
34&gt;
35&gt; &gt;
36&gt;
37&gt; &gt; //--------------------------------------------------------------------------------------
38&gt;
39&gt; &gt; //{html_select_date}不用处理会自动输出
40&gt;
41&gt; &gt; //--------------------------------------------------------------------------------------
42&gt;
43&gt; &gt;
44&gt;
45&gt; &gt; $smarty-&gt;display("example3.tpl");
46&gt;
47&gt; &gt;
>
> >
>
> >
>
> >
>
> > ======================================================
>
> > example3.php输出效果:
>
> > ======================================================
>
> >>
> >
> > >
> > >
> > {assign var="UserName" value="大师兄"} >
> > 这里将显示模板内部定义的一个变量:UserName = 大师兄 >
> > >
> > 下面的这一行将显示3个checkBox:
>
> > 语文
>
> > 数学
>
> > 外语
>
> > 下面在这一行将显示3个radio:
>
> > 语文
>
> > 数学
>
> > 外语
>
> > 下面显示一个月,日, 年选择框:
>
> > >
> > >
> > >
> >
CopyRight(C) By XiaoJun, Li 2004ÁªÏµ×÷Õß >
> > >
> >>
> > >
> > 例3使用了一些smarty模板中内置的一些函数,相似的函数大家可以在手册中查到,使用方法很简单,大家可以自己去查找. >
> > >
> >
>
> > 例4.模板控制(if / elseif / else/ endif ) >
> > ======================================================= >
> > example4.tpl >
> > ======================================================= >
> >>
> >
> > >
> >
{ $News[loop].newsID} | >{ $News[loop].newsTitle} | >
> > >
> >>
> > >
> > >
> > ======================================================= >
> > example4.php >
> > ======================================================= >
> > ```
>
> > /*********************************************
>
> > *
>
> > * 文件名: example4.php
>
> > * 作 用: 显示实例程序4
>
> > *
>
> > * 作 者: 大师兄
>
> > * Email: [email protected]
>
> > *
>
> > *********************************************/
>
> >
>
> > require_once ("./public/inc/F117_Smarty.php");
>
> >
>
> > $smarty = new F117_Smarty;
>
> > $smarty->template_dir = './templates/';
>
> > $smarty->compile_dir = './templates_c/';
>
> > $smarty->config_dir = './configs/';
>
> > $smarty->cache_dir = './cache/';
>
> > $smarty->caching = false;
>
> >
>
> > $array[]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
>
> > $array[]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
>
> > $array[]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
>
> > $array[]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
>
> > $array[]= array("newsID"=>"005", "newsTitle"=>"第5条新闻");
>
> > $array[]= array("newsID"=>"006", "newsTitle"=>"第6条新闻");
>
> > $array[]= array("newsID"=>"007", "newsTitle"=>"第7条新闻");
>
> > $array[]= array("newsID"=>"008", "newsTitle"=>"第8条新闻");
>
> >
>
> >
>
> > $smarty->assign("News", $array);
>
> >
>
> > $smarty->display("example4.tpl");
>
> >
1>
2> >
3>
4> > ==================================================
5>
6> > example4.php输出:
7>
8> > ==================================================
9>
10> > <html>
11>
12> > <head><title>模板中的流程控制</title><head>
13>
14> > <body>
15>
16> > <table border="1">
17>
18> >
19>
20> > <tr bgcolor="#D4D0C8">
21>
22> >
23>
24> > <td>001</td>
25>
26> > <td>第1条新闻</td>
27>
28> > </tr>
29>
30> > <tr bgcolor="#EEEEEE">
31>
32> >
33>
34> > <td>002</td>
35>
36> > <td>第2条新闻</td>
37>
38> > </tr>
39>
40> > <tr bgcolor="#D4D0C8">
41>
42> >
43>
44> > <td>003</td>
45>
46> > <td>第3条新闻</td>
47>
48> > </tr>
49>
50> > <tr bgcolor="#EEEEEE">
51>
52> >
53>
54> > <td>004</td>
55>
56> > <td>第4条新闻</td>
57>
58> > </tr>
59>
60> > <tr bgcolor="#D4D0C8">
61>
62> >
63>
64> > <td>005</td>
65>
66> > <td>第5条新闻</td>
67>
68> > </tr>
69>
70> > <tr bgcolor="#EEEEEE">
71>
72> >
73>
74> > <td>006</td>
75>
76> > <td>第6条新闻</td>
77>
78> > </tr>
79>
80> > <tr bgcolor="#D4D0C8">
81>
82> >
83>
84> > <td>007</td>
85>
86> > <td>第7条新闻</td>
87>
88> > </tr>
89>
90> > <tr bgcolor="#EEEEEE">
91>
92> >
93>
94> > <td>008</td>
95>
96> > <td>第8条新闻</td>
97>
98> > </tr>
99>
100> > </table>
101>
102> > </body>
103>
104> > </head></head></html>
105>
106> >
107>
108> > 模板文件中使用:
109>
110> > {if $tbColor == "#D4D0C8"}
111>
112> > <tr bgcolor="{ $tbColor}">
113>
114> > {assign var="tbColor" value="#EEEEEE"}
115>
116> > {else $tbColor == "#EEEEEE"}
117>
118> > <tr bgcolor="{ $tbColor}">
119>
120> > {assign var="tbColor" value="#D4D0C8"}
121>
122> > {/if}
123>
124> > 这一语句块进行设置每一行的背景颜色, {assign var="tbColor" value="#D4D0C8"}还记的吧,是例3中设置模板内部变量的定义方法,
125>
126> > 使用模板内置 的流程控制语句有时可以极大程度上提高程序的控制能力,下面一个例子是phpx.com中曾经有位朋友问过的,我将它作为
127>
128> > 实例放在这里供大家学习.
129>
130> >
131>
132> >
133>
134> >
135>
136> > 例5: 使用模板内置流程控制语句进行一行多单元格内容输出, 也就是在视觉上smarty每记输出几条记录:
137>
138> > ================================================
139>
140> > example5.tpl
141>
142> > ================================================
143>
144> > <html>
145>
146> > <head><title>一行输出多条记录</title></head>
147>
148> > <body>
149>
150> > <table>
151>
152> > <tr>
153>
154> > {section name=loop loop= $News step=1}
155>
156> > {if $smarty.section.loop.index % 4 == 0}
157>
158> > </tr>
159>
160> > <tr>
161>
162> > {/if}
163>
164> > <td>{ $News[loop].newsID}</td>
165>
166> > <td>{ $News[loop].newsTitle}</td>
167>
168> > {/section}
169>
170> > </tr>
171>
172> > </table>
173>
174> > </body>
175>
176> > </html>
177>
178> >
179>
180> >
181>
182> > ====================================================
183>
184> > example5.php
185>
186> > ====================================================
187>
188> > ```
189
190&gt;
191&gt; &gt; /*********************************************
192&gt;
193&gt; &gt; *
194&gt;
195&gt; &gt; * 文件名: example5.php
196&gt;
197&gt; &gt; * 作 用: 显示实例程序5
198&gt;
199&gt; &gt; *
200&gt;
201&gt; &gt; * 作 者: 大师兄
202&gt;
203&gt; &gt; * Email: [email protected]
204&gt;
205&gt; &gt; *
206&gt;
207&gt; &gt; *********************************************/
208&gt;
209&gt; &gt;
210&gt;
211&gt; &gt; require_once ("./public/inc/F117_Smarty.php");
212&gt;
213&gt; &gt;
214&gt;
215&gt; &gt; $smarty = new F117_Smarty;
216&gt;
217&gt; &gt; $smarty-&gt;template_dir = './templates/';
218&gt;
219&gt; &gt; $smarty-&gt;compile_dir = './templates_c/';
220&gt;
221&gt; &gt; $smarty-&gt;config_dir = './configs/';
222&gt;
223&gt; &gt; $smarty-&gt;cache_dir = './cache/';
224&gt;
225&gt; &gt; $smarty-&gt;caching = false;
226&gt;
227&gt; &gt;
228&gt;
229&gt; &gt; $array[]= array("newsID"=&gt;"001", "newsTitle"=&gt;"第1条新闻");
230&gt;
231&gt; &gt; $array[]= array("newsID"=&gt;"002", "newsTitle"=&gt;"第2条新闻");
232&gt;
233&gt; &gt; $array[]= array("newsID"=&gt;"003", "newsTitle"=&gt;"第3条新闻");
234&gt;
235&gt; &gt; $array[]= array("newsID"=&gt;"004", "newsTitle"=&gt;"第4条新闻");
236&gt;
237&gt; &gt; $array[]= array("newsID"=&gt;"005", "newsTitle"=&gt;"第5条新闻");
238&gt;
239&gt; &gt; $array[]= array("newsID"=&gt;"006", "newsTitle"=&gt;"第6条新闻");
240&gt;
241&gt; &gt; $array[]= array("newsID"=&gt;"007", "newsTitle"=&gt;"第7条新闻");
242&gt;
243&gt; &gt; $array[]= array("newsID"=&gt;"008", "newsTitle"=&gt;"第8条新闻");
244&gt;
245&gt; &gt;
246&gt;
247&gt; &gt;
248&gt;
249&gt; &gt; $smarty-&gt;assign("News", $array);
250&gt;
251&gt; &gt;
252&gt;
253&gt; &gt; $smarty-&gt;display("example5.tpl");
254&gt;
255&gt; &gt;
>
> >
>
> > ==================================================
>
> > example5.php输出内容:
>
> > ==================================================
>
> > >
> >
> > >
> >
001 | >第1条新闻 | >002 | >第2条新闻 | >003 | >第3条新闻 | >004 | >第4条新闻 | >
005 | >第5条新闻 | >006 | >第6条新闻 | >007 | >第7条新闻 | >008 | >第8条新闻 | >
> > >
> > >
> > >
> > 说明:本来还可以优化,使得第一行不输出一个空行的
> > {section name=loop loop= $News step=1} >
> > {if $smarty.section.loop.index % 4 == 0} >
> > >
> >
> > {/if} >
> >
> >
> > {/section} >
> > >
> > {section}{/section}指的是一个循环部分,在下一节会有详细的介绍,我们主要来看看这一句: >
> > {if $smarty.section.loop.index % 4 == 0} >
> > $smarty.section.loop指出 $smarty的实例中的section段有一个叫loop的部分, 它有一个属性叫index, 它的表示当前循环的索引值, >
> > 从0开始递增, 我们把它%4后与0相比较,也就是说,如果当前的索引值是4的倍数,它就输出一个
> > 很简单的就解决了一个在程序上实现起来很麻烦的事情. >