1. 简单例子 有助回忆基本知识点
1 define("DIR",dirname(__FILE__)); 2 require_once(DIR."/libs/Smarty.class.php");//引用smarty相对路径 3 4 $smarty = new Smarty(); 5 $smarty->template_dir(DIR."/templates");//模板路径 6 $smarty->complie_dir(DIR."/templates_c");//编译模板的路径 7 $smarty->cache_dir(DIR."/cache");//smarty 缓存目录 8 $smarty->caching = true; //true启用缓存 false禁止使用缓存 9 $smarty->cache_lifetime=3600*24;/缓存页面24小时 10 $smarty->left_delimiter="<{"; 11 $smarty->right_delimiter="}>";//左右分隔符
以上变量均可以在Smarty.class.php中找到,可以在那里进行全局设置。 也可以对某个模板如主页index.html进行单独配置。
12 13 $smarty->assign("xx", "xxx");//分配smarty变量,编译模板进行解析 14 $smarty->display("index.html");//显示解析后的模板
2.重点foreach使用,多级嵌套伪代码,文章分页列表页:page title date 分别表示:页码, 文章标题,发布时间
index.php
$article[0] = array("我是第1篇文章的标题"=>2011,"我是第2篇文章的标题"=>2011,"我是第3篇文章的标题"=>2011); $article[1] = array("我是第4篇文章的标题"=>2012,"我是第5篇文章的标题"=>2012,"我是第6篇文章的标题"=>2012); $article[2] = array("我是第7篇文章的标题"=>2013,"我是第8篇文章的标题"=>2013,"我是第9篇文章的标题"=>2013); $smarty->assign("article", $article); $smarty->display("index.html");
index.html
{foreach from=$article key=page item=title_date}
{$page}页有以下这些文章:
{foreach from=$title_date key=title item=date}
文章标题:{$title}; 发布时间:{$date}
{/foreach}
{foreachelse}
此{$page}无文章
{/foreach}
问题1:
通常,使用循环时候是为了得到value值,所以,经常看到省略写法也是正确的。{foreach from=$article item=value} 。但是,此处需要用到key值。
3.重点section 使用
{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 name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br> {/section} OUTPUT: 0 id: 1000<br> 1 id: 1001<br> 2 id: 1002<br> index_prev 属性演示 {section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br> {* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *} {if $custid[customer.index_prev] ne $custid[customer.index]} The customer id changed<br> {/if} {/section} OUTPUT: 0 id: 1000<br> The customer id changed<br> 1 id: 1001<br> The customer id changed<br> 2 id: 1002<br> The customer id changed<br> iteration 属性演示 iteration 不像index属性受start、step和max属性的影响,该值总是从1开始(index是从0开始的).rownum 是iteration的别名,两者等同. {section name=customer loop=$custid start=5 step=2} current loop iteration: {$smarty.section.customer.iteration}<br> {$smarty.section.customer.index} id: {$custid[customer]}<br> {* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *} {if $custid[customer.index_next] ne $custid[customer.index]} The customer id will change<br> {/if} {/section} OUTPUT: current loop iteration: 1 5 id: 1000<br> The customer id will change<br> current loop iteration: 2 7 id: 1001<br> The customer id will change<br> current loop iteration: 3 9 id: 1002<br> The customer id will change<br> first 属性演示 如果当前循环第一次执行,first 被设置为true. {section name=customer loop=$custid} {if $smarty.section.customer.first} <table> {/if} <tr><td>{$smarty.section.customer.index} id: {$custid[customer]}</td></tr> {if $smarty.section.customer.last} </table> {/if} {/section} OUTPUT: <table> <tr><td>0 id: 1000</td></tr> <tr><td>1 id: 1001</td></tr> <tr><td>2 id: 1002</td></tr> </table> 遍历多维数组: {section name=customer loop=$contacts} name: {$contacts[customer].name}<br> home: {$contacts[customer].home}<br> cell: {$contacts[customer].cell}<br> e-mail: {$contacts[customer].email}<p> {/section} loop 用于显示该循环上一次循环时的索引值. 该值可以用于循环内部或循环结束后. {$smarty.section.customer.loop} total 用于显示循环执行总的次数. 可以在循环中或执行结束后调用此属性. {$smarty.section.customer.total} Section 循环 通过如下方式调用变量{$smarty.section.sectionname.varname}.