php smarty section loop

==========================================example6.php
==========================================
<?php
/*********************************************
*
* 文件名: example6.php
* 作 用: 显示实例程序2
*
* 作 者: 大师兄
* Email: teacherli@163.com
*
*********************************************/
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("example6.tpl"); 
?>

=================================================
example6.php 输出文件
=================================================
<html>
<head><title>foreach使用的例子</title></head>
<body>
这里将输出一个数组:<br>

新闻编号:1<br>
新闻内容:第1条新闻<br><hr>

新闻编号:2<br>
新闻内容:第2条新闻<br><hr>

新闻编号:3<br>
新闻内容:第3条新闻<br><hr>

新闻编号:4<br>
新闻内容:第4条新闻<br><hr>

新闻编号:5<br>
新闻内容:第5条新闻<br><hr>

新闻编号:6<br>
新闻内容:第6条新闻<br><hr>
</body>
</html>

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
===========================================
<html>
<head><title>这是一个foreach使用的例子</title></head>
<body>
这里将输出一个数组:<br>
<{section name=loop loop=$News}>
新闻编号:<{$News[loop].newsID}><br>
新闻标题:<{$News[loop].newsTitle}><br><hr>
<{sectionelse}>
对不起,没有任何新闻输入!
<{/section}>
</body>
</html>

==========================================
example7.php
==========================================
<?php
/*********************************************
*
* 文件名: example7.php
* 作 用: 显示实例程序2
*
* 作 者: 大师兄
* Email: teacherli@163.com
*
*********************************************/
include_once("./comm/Smarty.class.php"); 

$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->template_dir = "./templates";//设置模板目录
$smarty->compile_dir = "./templates_c"; //设置编译目录

$smarty->cache_dir = "./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("News", $array);

//编译并显示位于./templates下的index.tpl模板
$smarty->display("example7.tpl"); 
?>

=================================================
example7.php 输出文件
=================================================
<html>
<head><title>foreach使用的例子</title></head>
<body>
这里将输出一个数组:<br>

新闻编号:1<br>
新闻内容:第1条新闻<br><hr>

新闻编号:2<br>
新闻内容:第2条新闻<br><hr>

新闻编号:3<br>
新闻内容:第3条新闻<br><hr>

新闻编号:4<br>
新闻内容:第4条新闻<br><hr>

新闻编号:5<br>
新闻内容:第5条新闻<br><hr>

新闻编号:6<br>
新闻内容:第6条新闻<br><hr>
</body>
</html>

这里的{section}块的对于变量的命名方式感觉有些别扭,不过没关系,你只要记住模板变量使用:
$loopName[name].var这种模式就行了,loopName为loop处赋予的变量名,[name]为name处赋予的字符串,.后为为你要在程序数组中设定要与值相对应的下标名称就行了。


好了,smarty学习指南---程序设计篇就写到这里,对于一般的应用,这些知识已经够用了,其它的一些高级技巧大家请参看手册中的例子,下一节将讲讲Smarty在实际应用中的例子,将分别以php内置的mysql语句,phplib中的DB类,来分别讲一下各个类库在同一个例子中的实现。

http://blog.163.com/alex_kame/blog/static/1454674820094611141679/


原文地址:https://www.cnblogs.com/derrck/p/1510677.html