test.tpl
1
2{section name=outer loop=$FirstName}3{if $smarty.section.outer.index is odd by 3}45 {$smarty.section.outer.index}-{$smarty.section.outer.rownum} . {$FirstName[outer]} {$LastName[outer]}67{else}89 {$smarty.section.outer.index}-{$smarty.section.outer.rownum} * {$FirstName[outer]} {$LastName[outer]}1011{/if}12{sectionelse}13 none14{/section}1516
test.php
1
<?php2 3require'lib/Smarty-2.6.11/libs/Smarty.class.php';45$smarty=newSmarty;67//$smarty->compile_check = true;8//$smarty->debugging = true;910//$smarty->assign("hello","byby");11$smarty->assign("FirstName",array("John","Mary","James","Henry","Tom","Hello"));1213$smarty->display('test.tpl');1415?>
index
index用来显示当前循环的指针,从0开始.
index_prev
用来显示前一次循环的指针,从-1开始
index_next
用来显示后一次循环的指针.
{if $smarty.section.outer.index is odd by 1}0 -1 * John
1 -2 . Mary
2 -3 * James
3 -4 . Henry
4 -5 * Tom
5 -6 . Hello
0/1=0 非奇非偶,执行else项
1/1=1 奇数,执行if项
2/1=2 偶数,执行else项
3/1=3 奇数,执行if项
4/1=4 偶数,执行else项
5/1=5 奇数,执行if项
{if $smarty.section.outer.index is odd by 2}0 -1 * John
1 -2 * Mary
2 -3 . James
3 -4 . Henry
4 -5 * Tom
5 -6 * Hello
0/2=0 非奇非偶,执行else项
1/2=0 非奇非偶,执行else项
2/2=1 奇数,执行if项
3/2=1 奇数,执行if项
4/2=2 偶数,执行else项
5/2=2 奇数,执行else项
{if $smarty.section.outer.index is odd by 3}0 -1 * John
1 -2 * Mary
2 -3 * James
3 -4 . Henry
4 -5 . Tom
5 -6 . Hello
0/3=0 非奇非偶,执行else项
1/3=0 非奇非偶,执行else项
2/3=0 非奇非偶,执行else项
3/3=1 奇数,执行if项
4/3=1 奇数,执行if项
5/3=1 奇数,执行else项