手机
当前位置:查字典教程网 >编程开发 >php教程 >smarty实例教程
smarty实例教程
摘要:smarty实例教程(1)一、什么是smarty?smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单...

smarty实例教程(1)

一、什么是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变量识符里的用*包含的文字为注释内容*}

{includefile="header.tpl"}{*页面头*}

大家好,我叫{$name},欢迎大家阅读我的smarty学习材料。

{includefile="foot.tpl"}{*页面尾*}

上边的这个例子是一个tpl模板,其中:

1.{**}是模板页的注释,它在smarty对模板进行解析时不进行任何输出,仅供模板设计师对模板进行注释。

2.{includefile="xxx.tpl"}使用此句将一个模板文件包含到当前页面中,例子中将在网站中公用事的head.tpl与foot.tpl进行了包含,你可以

这样想,使用这一句将xxx.tpl中的内容全部复制在当前语句处。当然,你不使用这一句也可以,将XXX.tpl中的内容复制到当前语句处

也是完全可以了。

3.{$name}:模板变量,smarty中的核心组成,采用smarty定义的左边界符{与右边界符}包含着、以PHP变量形式给出,在smarty程序中将使用

$smarty->assign("name","李晓军");将模板中的$name替换成“李晓军”三个字。

整个实例源程序如下:

=============================

header.tpl

=============================

<html>

<head>

<title>大师兄smarty教程</title>

</head>

<body>

===============================

foot.tpl

===============================

<hr>

<center>CopyRight(C)by大师兄2004年8月</center>

<hr>

</body>

</html>

=====================================================

index.tpl

=====================================================

{*显示是smarty变量识符里的用*包含的文字为注释内容*}

{includefile="header.tpl"}{*页面头*}

大家好,我叫{$name},欢迎大家阅读我的smarty学习材料。

{includefile="foot.tpl"}{*页面尾*}

================================================

index.php

================================================

<?php

include_once("./comm/Smarty.class.php");//包含smarty类文件

$smarty=newSmarty();//建立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");

?>

最终执行这个程序时将显示为:

================================

执行index.php

================================

<html>

<head>

<title>大师兄smarty教程</title>

</head>

<body>

大家好,我叫李晓军,欢迎大家阅读我的smarty学习材料。

<hr>

<center>CopyRight(C)by大师兄2004年8月</center>

<hr>

</body>

</html>

smarty实例教程(2)

这个例子是综合使用smarty模板参数的一个例子,这些参数用来控制模板的输出,我只选其中几个,其它的参数你去看参考吧。

================================================

exmple2.tpl

================================================

<html>

<head><title>大师兄smarty示例2</title></head>

<body>

1.第一句首字母要大写:{$str1|capitalize}<br>

2.第二句模板变量+李晓军:{$str2|cat:"李晓军"}<br>

3.第三句输出当前日期:{$str3|date_format:"%Y年%m月%d日"}

4.第四句.php程序中不处理,它显示默认值:{$str4|default:"没有值!"}

5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:<br>

{$str5|indent:8:"*"}}<br>

6.第六句把TEACHerLI@163.com全部变为小写:{$str6|lower}<br>

7.第七句把变量中的teacherli替换成:李晓军:{$str7|replace:"teacherli":"李晓军"}<br>

8.第八句为组合使用变量修改器:{$str8|capitalize|cat:"这里是新加的时间:"|date_format:"%Y年%m月%d日"|lower}

</body>

</html>

===============================================

example2.php

===============================================

<?php

include_once("./Smarty.class.php");//包含smarty类文件

$smarty=newSmarty();//建立smarty实例对象$smarty

$smarty->templates("./templates");//设置模板目录

$smarty->templates_c("./templates_c");//设置编译目录

//----------------------------------------------------

//左右边界符,默认为{},但实际应用当中容易与Javascript

//相冲突,所以建议设成<{}>或其它。

//----------------------------------------------------

$smarty->left_delimiter="{";

$smarty->right_delimiter="}";

$smarty->assign("str1","mynameisxiaojun,li.");//将str1替换成MyNameIsXiaoJun,Li.

$smarty->assign("str2","我的名字叫:");//输出:我的名字叫:李晓军

$smarty->assign("str3","公元");//输出公元2004年8月21日(我的当前时间)

//$smarty->assign("str4","");//第四句不处理时会显示默认值,如果使用前面这一句则替换为""

$smarty->assign("str5","前边8个*");//第五句输出:********前边8个*

$smarty->assign("str6","TEACHerLI@163.com");//这里将输出teacherli@163.com

$smarty->assign("str7","thisisteacherli");//在模板中显示为:thisis李晓军

$smarty->assign("str8","HEREISCOMBINING:");

//编译并显示位于./templates下的index.tpl模板

$smarty->display("example2.tpl");

?>

最终输出效果:

======================================================

example2.php输出效果:

======================================================

<html>

<head><title>大师兄smarty示例2</title></head>

<body>

1.第一句首字母要大写:MyNameIsXiaoJun,Li.<br>

2.第二句模板变量+李晓军:我的名字叫:李晓军<br>

3.第三句输出当前日期:公元2004年8月21日<br>

4.第四句.php程序中不处理,它显示默认值:没有值!<br>

5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:<br>

********前边8个*<br>

6.第六句把TEACHerLI@163.com全部变为小写:teacherli@163.com<br>

7.第七句把变量中的teacherli替换成:李晓军:thisis李晓军<br>

8.第八句为组合使用变量修改器:HereisCombining:这里是新加的时间:2004年8月21日

</body>

</html>

在模板中的这些参数被称为变量修改器(variablemodifiers),使用这些参数可对模板进行一系列的修改控制。变量修改器

使用"|"和调节器名称应用修改器,使用":"分开修改器参数。变量修改器可以组合使用,像第八句一样,实际使用中可以灵活应用。

实例3.

==================================================

example3.tpl

==================================================

<html>

<head><title>模板中内定的一些函数</title></head>

<body>

{*下面的这一段相当于在模板内部定义一个变量UserName*}

{assignvar="UserName"value="大师兄"}

这里将显示模板内部定义的一个变量:UserName=admin

下面的这一行将显示3个checkBox:<br>

{html_checkboxesname="CheckBox"values=$CheckNamechecked=$IsCheckedoutput=$valueseparator="<br/>"}

下面在这一行将显示3个radio:<br>

{html_radioesname="RadioBox"values=$RadioNamechecked=$IsCheckedoutput=$valueseparator="<br/>"}

下面显示一个月,日,年选择框:<br>

{html_select_date}

<hr><b>CopyRight(C)ByXiaoJun,Li2004<b>{mailtoaddress="teacherli@163.ccom"text="联系作者"}

</body>

</html>

======================================================

example3.php

======================================================

<?php

require_once("./comm/Smarty.class.php");

$smarty=newF117_Smarty;

$smarty->template_dir='./templates/';

$smarty->compile_dir='./templates_c/';

$smarty->config_dir='./configs/';

$smarty->cache_dir='./cache/';

$smarty->caching=false;

//--------------------------------------------------------------------------------------

//处理{html_checkboxesname="CheckBox"values=$CheckNamechecked=$IsCheckedoutput=$valueseparator="<br/>"}

//--------------------------------------------------------------------------------------

$smarty->assign('CheckName',array(

1001=>'语文',

1002=>'数学',

1003=>'外语'));

$smarty->assign('IsChecked',1001);

//--------------------------------------------------------------------------------------

//处理{html_radioesname="RadioBox"values=$RadioNamechecked=$IsCheckedoutput=$valueseparator="<br/>"}

//--------------------------------------------------------------------------------------

$smarty->assign('RadioName',array(

1001=>'语文',

1002=>'数学',

1003=>'外语'));

$smarty->assign('IsChecked',1001);

//--------------------------------------------------------------------------------------

//{html_select_date}不用处理会自动输出

//--------------------------------------------------------------------------------------

$smarty->display("example3.tpl");

?>

smarty实例教程(3)

======================================================

example3.php输出效果:

======================================================

<html>

<head><title>模板中内定的一些函数</title></head>

<body>

{assignvar="UserName"value="大师兄"}

这里将显示模板内部定义的一个变量:UserName=大师兄

下面的这一行将显示3个checkBox:<br>

<inputtype="checkbox"name="CheckBox[]"value="1000">语文<br/>

<inputtype="checkbox"name="CheckBox[]"value="1001"checked="checked">数学<br/>

<inputtype="checkbox"name="CheckBox[]"value="1002">外语<br/>

下面在这一行将显示3个radio:<br>

<inputtype="radio"name="RadioBox[]"value="1000">语文<br/>

<inputtype="radio"name="RadioBox[]"value="1001"checked="checked">数学<br/>

<inputtype="radio"name="RadioBox[]"value="1002">外语<br/>

下面显示一个月,日,年选择框:<br>

<selectname="Date_Month">

<optionlabel="January"value="01">January</option>

<optionlabel="February"value="02">February</option>

<optionlabel="March"value="03">March</option>

<optionlabel="April"value="04">April</option>

<optionlabel="May"value="05">May</option>

<optionlabel="June"value="06">June</option>

<optionlabel="July"value="07">July</option>

<optionlabel="August"value="08"selected="selected">August</option>

<optionlabel="September"value="09">September</option>

<optionlabel="October"value="10">October</option>

<optionlabel="November"value="11">November</option>

<optionlabel="December"value="12">December</option>

</select>

<selectname="Date_Day">

<optionlabel="01"value="1">01</option>

<optionlabel="02"value="2">02</option>

<optionlabel="03"value="3">03</option>

<optionlabel="04"value="4">04</option>

<optionlabel="05"value="5">05</option>

<optionlabel="06"value="6">06</option>

<optionlabel="07"value="7">07</option>

<optionlabel="08"value="8">08</option>

<optionlabel="09"value="9">09</option>

<optionlabel="10"value="10">10</option>

<optionlabel="11"value="11">11</option>

<optionlabel="12"value="12">12</option>

<optionlabel="13"value="13">13</option>

<optionlabel="14"value="14">14</option>

<optionlabel="15"value="15">15</option>

<optionlabel="16"value="16">16</option>

<optionlabel="17"value="17">17</option>

<optionlabel="18"value="18">18</option>

<optionlabel="19"value="19">19</option>

<optionlabel="20"value="20">20</option>

<optionlabel="21"value="21"selected="selected">21</option>

<optionlabel="22"value="22">22</option>

<optionlabel="23"value="23">23</option>

<optionlabel="24"value="24">24</option>

<optionlabel="25"value="25">25</option>

<optionlabel="26"value="26">26</option>

<optionlabel="27"value="27">27</option>

<optionlabel="28"value="28">28</option>

<optionlabel="29"value="29">29</option>

<optionlabel="30"value="30">30</option>

<optionlabel="31"value="31">31</option>

</select>

<selectname="Date_Year">

<optionlabel="2004"value="2004"selected="selected">2004</option>

</select>

<hr><b>CopyRight(C)ByXiaoJun,Li2004<b><ahref="mailto:teacherli@163.com">ÁªÏµ×÷Õß</a>

</body>

</html>

例3使用了一些smarty模板中内置的一些函数,相似的函数大家可以在手册中查到,使用方法很简单,大家可以自己去查找.

例4.模板控制(if/elseif/else/endif)

=======================================================

example4.tpl

=======================================================

<html>

<head><title>模板中的流程控制</title><head>

<body>

<tableborder="1">

{assignvar="tbColor"value="#D4D0C8"}

色彩:{$tbColor}<br>

{sectionname=looploop=$News}

{if$tbColor=="#D4D0C8"}

<trbgcolor="{$tbColor}">

{assignvar="tbColor"value="#EEEEEE"}

{else$tbColor=="#EEEEEE"}

<trbgcolor="{$tbColor}">

{assignvar="tbColor"value="#D4D0C8"}

{/if}

<td>{$News[loop].newsID}</td>

<td>{$News[loop].newsTitle}</td>

<tr>

{/section}

</table>

</body>

</html>

=======================================================

example4.php

=======================================================

<?php

require_once("./public/inc/F117_Smarty.php");

$smarty=newF117_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");

?>

smarty实例教程(4)

==================================================

example4.php输出:

==================================================

<html>

<head><title>模板中的流程控制</title><head>

<body>

<tableborder="1">

<trbgcolor="#D4D0C8">

<td>001</td>

<td>第1条新闻</td>

</tr>

<trbgcolor="#EEEEEE">

<td>002</td>

<td>第2条新闻</td>

</tr>

<trbgcolor="#D4D0C8">

<td>003</td>

<td>第3条新闻</td>

</tr>

<trbgcolor="#EEEEEE">

<td>004</td>

<td>第4条新闻</td>

</tr>

<trbgcolor="#D4D0C8">

<td>005</td>

<td>第5条新闻</td>

</tr>

<trbgcolor="#EEEEEE">

<td>006</td>

<td>第6条新闻</td>

</tr>

<trbgcolor="#D4D0C8">

<td>007</td>

<td>第7条新闻</td>

</tr>

<trbgcolor="#EEEEEE">

<td>008</td>

<td>第8条新闻</td>

</tr>

</table>

</body>

</html>

模板文件中使用:

{if$tbColor=="#D4D0C8"}

<trbgcolor="{$tbColor}">

{assignvar="tbColor"value="#EEEEEE"}

{else$tbColor=="#EEEEEE"}

<trbgcolor="{$tbColor}">

{assignvar="tbColor"value="#D4D0C8"}

{/if}

这一语句块进行设置每一行的背景颜色,{assignvar="tbColor"value="#D4D0C8"}还记的吧,是例3中设置模板内部变量的定义方法,

使用模板内置的流程控制语句有时可以极大程度上提高程序的控制能力,下面一个例子是phpx.com中曾经有位朋友问过的,我将它作为

实例放在这里供大家学习.

例5:使用模板内置流程控制语句进行一行多单元格内容输出,也就是在视觉上smarty每记输出几条记录:

================================================

example5.tpl

================================================

<html>

<head><title>一行输出多条记录</title></head>

<body>

<table>

<tr>

{sectionname=looploop=$Newsstep=1}

{if$smarty.section.loop.index%4==0}

</tr>

<tr>

{/if}

<td>{$News[loop].newsID}</td>

<td>{$News[loop].newsTitle}</td>

{/section}

</tr>

</table>

</body>

</html>

====================================================

example5.php

====================================================

<?php

require_once("./public/inc/F117_Smarty.php");

$smarty=newF117_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("example5.tpl");

?>

==================================================

example5.php输出内容:

==================================================

<html>

<head><title>一行输出多条记录</title></head>

<body>

<table>

<tr>

</tr>

<tr>

<td>001</td>

<td>第1条新闻</td>

<td>002</td>

<td>第2条新闻</td>

<td>003</td>

<td>第3条新闻</td>

<td>004</td>

<td>第4条新闻</td>

</tr>

<tr>

<td>005</td>

<td>第5条新闻</td>

<td>006</td>

<td>第6条新闻</td>

<td>007</td>

<td>第7条新闻</td>

<td>008</td>

<td>第8条新闻</td>

</tr>

</table>

</body>

</html>

说明:本来还可以优化,使得第一行不输出一个空行的<tr></tr>,但是学习程序,简单为好,先就这么用了.在这里说明一下:

{sectionname=looploop=$Newsstep=1}

{if$smarty.section.loop.index%4==0}

</tr>

<tr>

{/if}

<td>{$News[loop].newsID}</td>

<td>{$News[loop].newsTitle}</td>

{/section}

{section}{/section}指的是一个循环部分,在下一节会有详细的介绍,我们主要来看看这一句:

{if$smarty.section.loop.index%4==0}

$smarty.section.loop指出$smarty的实例中的section段有一个叫loop的部分,它有一个属性叫index,它的表示当前循环的索引值,

从0开始递增,我们把它%4后与0相比较,也就是说,如果当前的索引值是4的倍数,它就输出一个</tr><tr>,否则执行下面的部分,

很简单的就解决了一个在程序上实现起来很麻烦的事情.

【smarty实例教程】相关文章:

phpmyadmin操作流程

php中smarty变量修饰用法实例分析

PHP中使用curl入门教程

php注入实例

综合图片计数器

PHP中isset与array_key_exists的区别实例分析

PHP 和 MySQL 基础教程(二)

PHP 和 MySQL 基础教程(三)

php中memcache 基本操作实例

PHP mysql事务问题实例分析教程

精品推荐
分类导航