30分钟助你快速掌握Smarty PHP教程

2025-05-03 20:01:37

1、 在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计。下载Smarty文件放到你们站点中。index.php代码如下:<?php/**** @version $Id: index.php* @package* @author www.jb51.net* @action 显示实例程序*/include_once("./Smarty/Smarty.class.php"); //包含smarty类文件$smarty = new Smarty(); //建立smarty实例对象$smarty$smarty->templates("./templates"); //设置模板目录$smarty->templates_c("./templates_c"); //设置编译目录$smarty->cache("./cache"); //缓存目录$smarty->cache_lifetime = 0; //缓存时间$smarty->caching = true; //缓存方式$smarty->left_delimiter = "{#";$smarty->right_delimiter = "#}";$smarty->assign("name", "zaocha"); //进行模板变量替换$smarty->display("index.htm"); //编译并显示位于./templates下的index.htm模板?>

30分钟助你快速掌握Smarty PHP教程

3、接下来介绍一个section循环块与foreach循环块,本来它应该属于模板部分,但是由于它们是smarty的精华所在,而且与smarty程序设计部分联系非常紧密,所以就在本节单独拿出来讲一下。foreach:用于循环简单数组,它是一个选择性的section循环,它的定义格式为:{foreach from=$array item=array_id}{foreachelse}{/foreach}其中,from 指出要循环的数组变量,item为要循环的变量名称,循环次数由from所指定的数组变量的个数所决定。{foreachelse}用来当程序中传递过来的数组为空时的处理。

30分钟助你快速掌握Smarty PHP教程

5、这是一个错误的不显示墙绅褡孛数据,本文做了更正。程序文件:example.php如下:<?php/*忮氽阝另********************************************** 文件名: example.php* 作 用: 显示实例程序2*********************************************/include_once("./Smarty/Smarty.class.php");$smarty = new Smarty();$smarty->templates("./templates");$smarty->templates_c("./templates_c");$smarty->cache("./cache");$smarty->cache_lifetime = 0;$smarty->caching = true;$smarty->left_delimiter = "{#";$smarty->right_delimiter = "#}";$array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");$array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");$array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");$array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");$array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");$array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");//这是一个二维关联数组$smarty->assign("newsArray", $array);//编译并显示位于./templates下的index.htm模板$smarty->display("example.htm");?>

30分钟助你快速掌握Smarty PHP教程

7、section的产生是为解决foreach的不足的,与foreach一样,它用于设计模板内的循环块,它较为复杂,可极大程序上满足程序需要,所以在程序中我习惯使用它而不使用foreach,基本原形为:{section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]}参数解释如下:name: section的名称,不用加$$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。$start: 开始循环的下标,循环下标默认由0开始$step: 每次循环时下标的增数$max: 最大循环下标$show: boolean类型,决定是否对这个块进行显示,默认为true这里有个名词需要说明:循环下标:实际它的英文名称为index,是索引的意思,这里我将它译成"下标",主要是为了好理解。它表示在显示这个循环块时当前的循环索引,默认从0开始,受$start的影响,如果将$start设为5,它也将从5开始计数,在模板设计部分我们使用过它,这是当前{section}的一个属性,调用方式为Smarty.section.sectionName.index,这里的sectionName指的是函数原型中的name属性。{section}块具有的属性值,分别为:1. index: 上边我们介绍的"循环下标",默认为02. index_prev: 当前下标的前一个值,默认为-13. index_next: 当前下标的下一个值,默认为14. first: 是否为第一下循环5. last: 是否为最后一个循环6. iteration: 循环次数7. rownum: 当前的行号,iteration的另一个别名8. loop: 最后一个循环号,可用在section块后统计section的循环次数9. total: 循环次数,可用在section块后统计循环次数10. show: 在函数的声明中有它,用于判断section是否显示它们的具体属性大家可以参考手册,在程序中可灵活使用它的这些属性,模板部分我就使用过index属性,大家可以回过头去看看。同样,{section}也可以配合使用{sectionelse},用来表示传入的数组变量为空时对模板进行的处理。

30分钟助你快速掌握Smarty PHP教程

9、example.php文件如下:<?php/*****************************忮氽阝另***************** 文件名: example7.php* 作 用: 显示实例程序2*********************************************/include_once("./comm/Smarty.class.php");$smarty = new Smarty();$smarty->templates("./templates");$smarty->templates_c("./templates_c");$smarty->cache("./cache");$smarty->cache_lifetime = 0;$smarty->caching = true;$smarty->left_delimiter = "{";$smarty->right_delimiter = "}";$array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");$array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");$array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");$array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");$array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");$array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");$smarty->assign("newsArray", $array);//编译并显示位于./templates下的index.tpl模板$smarty->display("example.tpl");?>

30分钟助你快速掌握Smarty PHP教程
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢