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变量识符里的用*包含的文字为注释内容 *} {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 ============================= =============================== foot.tpl =============================== ===================================================== index.tpl ===================================================== {* 显示是smarty变量识符里的用*包含的文字为注释内容 *} {include file="header.tpl"}{*页面头*} 大家好,我叫{$name}, 欢迎大家阅读我的smarty学习材料。 {include file="foot.tpl"}{*页面尾*} ================================================ index.php ================================================ 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"); ?> 最终执行这个程序时将显示为: ================================ 执行index.php ================================ 大家好,我叫李晓军, 欢迎大家阅读我的smarty学习材料。 smarty实例教程(2) 这个例子是综合使用smarty模板参数的一个例子,这些参数用来控制模板的输出,我只选其中几个,其它的参数你去看参考吧。 ================================================ exmple2.tpl ================================================ 1. 第一句首字母要大写:{$str1|capitalize} 2. 第二句模板变量 + 李晓军:{$str2|cat:"李晓军"} 3. 第三句输出当前日期:{$str3|date_format:"%Y年%m月%d日"} 4. 第四句.php程序中不处理,它显示默认值:{$str4|default:"没有值!"} 5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符: {$str5|indent:8:"*"}} 6. 第六句把TEACHerLI@163.com全部变为小写:{$str6|lower} 7. 第七句把变量中的teacherli替换成:李晓军:{$str7|replace:"teacherli":"李晓军"} 8. 第八句为组合使用变量修改器:{$str8|capitalize|cat:"这里是新加的时间:"|date_format:"%Y年%m月%d日"|lower} =============================================== example2 .php =============================================== 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", "TEACHerLI@163.com"); //这里将输出teacherli@163.com $smarty->assign("str7", "this is teacherli"); //在模板中显示为:this is 李晓军 $smarty->assign("str8", "HERE IS COMBINING:"); //编译并显示位于./templates下的index.tpl模板 $smarty->display("example2.tpl"); ?> 最终输出效果: ====================================================== example2.php输出效果: ====================================================== 1. 第一句首字母要大写:My Name Is Xiao Jun, Li. 2. 第二句模板变量 + 李晓军:我的名字叫:李晓军 3. 第三句输出当前日期:公元2004年8月21日 4. 第四句.php程序中不处理,它显示默认值:没有值! 5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符: ********前边8个* 6. 第六句把TEACHerLI@163.com全部变为小写:teacherli@163.com 7. 第七句把变量中的teacherli替换成:李晓军:this is 李晓军 8. 第八句为组合使用变量修改器:Here is Combining:这里是新加的时间:2004年8月21日 在模板中的这些参数被称为变量修改器(variable modifiers),使用这些参数可对模板进行一系列的修改控制。变量修改器 使用"|"和调节器名称应用修改器, 使用":"分开修改器参数。变量修改器可以组合使用,像第八句一样,实际使用中可以灵活应用。 实例3. ================================================== example3.tpl ================================================== {*下面的这一段相当于在模板内部定义一个变量UserName*} {assign var="UserName" value="大师兄"} 这里将显示模板内部定义的一个变量:UserName = {$UserName} 下面的这一行将显示3个checkBox: {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="teacherli@163.ccom" text="联系作者"} ====================================================== example3.php ====================================================== 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=" "} //-------------------------------------------------------------------------------------- $smarty->assign('CheckName', array( 1001 => '语文', 1002 => '数学', 1003 => '外语')); $smarty->assign('IsChecked', 1001); //-------------------------------------------------------------------------------------- //处理{html_radioes name="RadioBox" values=$RadioName checked=$IsChecked output=$value separator=" "} //-------------------------------------------------------------------------------------- $smarty->assign('RadioName', array( 1001 => '语文', 1002 => '数学', 1003 => '外语')); $smarty->assign('IsChecked', 1001); //-------------------------------------------------------------------------------------- //{html_select_date}不用处理会自动输出 //-------------------------------------------------------------------------------------- $smarty->display("example3.tpl"); ?> smarty实例教程(3) ====================================================== 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 =======================================================
======================================================= example4.php ======================================================= 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"); ?> smarty实例教程(4) ================================================== example4.php输出: ==================================================
模板文件中使用: {if $tbColor == "#D4D0C8"} {assign var="tbColor" value="#EEEEEE"} {else $tbColor == "#EEEEEE"} {assign var="tbColor" value="#D4D0C8"} {/if} 这一语句块进行设置每一行的背景颜色, {assign var="tbColor" value="#D4D0C8"}还记的吧,是例3中设置模板内部变量的定义方法, 使用模板内置 的流程控制语句有时可以极大程度上提高程序的控制能力,下面一个例子是phpx.com中曾经有位朋友问过的,我将它作为 实例放在这里供大家学习. 例5: 使用模板内置流程控制语句进行一行多单元格内容输出, 也就是在视觉上smarty每记输出几条记录: ================================================ example5.tpl ================================================
==================================================== example5.php ==================================================== 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("example5.tpl"); ?> ================================================== example5.php输出内容: ==================================================
说明:本来还可以优化,使得第一行不输出一个空行的 {section name=loop loop=$News step=1} {if $smarty.section.loop.index % 4 == 0} | ||||||||||||||||||||||||||||||||||||
{$News[loop].newsID} | {$News[loop].newsTitle} | |||||||||||||||||||||||||||||||||||
|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )
GMT+8, 2024-9-30 01:29 , Processed in 0.167324 second(s), 12 queries , Gzip On, MemCache On.
Powered by Discuz! X3.5
© 2001-2023 Discuz! Team.