smarty
smarty的程序设计部分:
在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程
序设计。
php代码:--------------------------------------------------------------------------------
首先来介绍一下在上一节中我们使用的过的.php文件中的一些元素。同样,我们拿上一节中最开始的那个index.php文件来说明一下:
================================================
index.php
================================================
templates(./templates); //设置模板目录
$smarty->templates_c(./templates_c); //设置编译目录
//****大家注意,这里我是我新加入的****//
$smarty->cache(./cache); //设置缓存目录
$smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间
$smarty->caching = true; //设置缓存方式
//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与javascript
//相冲突,所以建议设成或其它。
//----------------------------------------------------
$smarty->left_delimiter = $smarty->right_delimiter = }>;
$smarty->assign(name, 李晓军); //进行模板变量替换
//编译并显示位于./templates下的index.tpl模板
$smarty->display(index.tpl);
?>
我们可以看到,smarty的程序部分实际就是符合php语言规范的一组代码,我们依次来解释一下:
1。/**/语句:
包含的部分为程序篇头注释。主要的内容应该为对程序的作用,版权与作者及编写时间做一个简单的介绍,这在smarty中不是必
需的,但从程序的风格来讲,这是一个好的风格。
2。include_once语句:
它将安装到网站的smarty文件包含到当前文件中,注意包含的路径一定要写正确。
3。$smarty = new smarty():
这一句新建一个smarty对象$smarty,简单的一个对象的实例化。
4。$smarty->templates():
这一句指明$smarty对象使用tpl模板时的路径,它是一个目录,在没有这一句时,smarty默认的模板路径为当前目录的templates
目录,实际在写程序时,我们要将这一句写明,这也是一种好的程序风格。
5。$smarty->templates_c():
这一句指明$smarty对象进行编译时的目录。在模板设计篇我们已经知道smarty是一种编译型模板语言,而这个目录,就是它编译
模板的目录,这里要注意,如果站点位于*nix服务器上,请确保teamplates_c里定义的这个目录具有可写可读权限,默认情况下它的编译目录
是当前目录下的templates_c,出于同样的理由我们将其明确的写出来。
6。$smarty->left_delimiter与$smarty->right_delimiter:
指明在查找模板变量时的左右分割符。默认情况下为{与},但在实际中因为我们要在模板中使用
==========================================
example6.php
==========================================
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(example6.tpl);
?>
=================================================
example6.php 输出文件
=================================================
foreach使用的例子
这里将输出一个数组:
新闻编号:1
新闻内容:第1条新闻
新闻编号:2
新闻内容:第2条新闻
新闻编号:3
新闻内容:第3条新闻
新闻编号:4
新闻内容:第4条新闻
新闻编号:5
新闻内容:第5条新闻
新闻编号:6
新闻内容:第6条新闻
foreach还可以用foreachelse来匹配,用foreachelse来表示当传递给foreach的数组为空值时程序要执行的操作,具体的使用方法,请参考
手册的说明。
2. section:
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: 上边我们介绍的循环下标,默认为0
2. index_prev: 当前下标的前一个值,默认为-1
3. index_next: 当前下标的下一个值,默认为1
4. first: 是否为第一下循环
5. last: 是否为最后一个循环
6. iteration: 循环次数
7. rownum: 当前的行号,iteration的另一个别名
8. loop: 最后一个循环号,可用在section块后统计section的循环次数
9. total: 循环次数,可用在section块后统计循环次数
10. show: 在函数的声明中有它,用于判断section是否显示
它们的具体属性大家可以参考手册,在程序中可灵活使用它的这些属性,模板部分我就使用过index属性,大家可以回过头去看看。
同样,{section}也可以配合使用{sectionelse},用来表示传入的数组变量为空时对模板进行的处理。
我们把上边的那个例子使用{section}来替代{foreach}来实现现样的功能,注意,在这个例子中我只将tpl模板中的{foreach}用
{section}来实现,php程序文件中没有任何改动,同时加了{sectionelse}处理块:
===========================================
example7.tpl
===========================================
这是一个foreach使用的例子
这里将输出一个数组:
{section name=loop, loop=$news}
新闻编号:{$news[loop].newsid}
新闻标题:{$news[loop].newstitle}
{sectionelse}
对不起,没有任何新闻输入!
{/section}
==========================================
example6.php
==========================================
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(example6.tpl);
?>
=================================================
example7.php 输出文件
=================================================
foreach使用的例子
这里将输出一个数组:
新闻编号:1
新闻内容:第1条新闻
新闻编号:2
新闻内容:第2条新闻
新闻编号:3
新闻内容:第3条新闻
新闻编号:4
新闻内容:第4条新闻
新闻编号:5
新闻内容:第5条新闻
新闻编号:6
新闻内容:第6条新闻
这里的{section}块的对于变量的命名方式感觉有些别扭,不过没关系,你只要记住模板变量使用:
$loopname[name].var这种模式就行了,loopname为loop处赋予的变量名,[name]为name处赋予的字符串,.后为为你要在程序数组中设定要
与值相对应的下标名称就行了。
好了,smarty学习指南---程序设计篇就写到这里,对于一般的应用,这些知识已经够用了,其它的一些高级技巧大家请参看手册中的例子
,下一节将讲讲smarty在实际应用中的例子,将分别以php内置的mysql语句,phplib中的db类,adodb,pear中db类来分别讲一下各个类库在同一个例子中的实现。