test.tpl
1
<
table
border
=1
>
2
{section name=outer loop=$FirstName}
3
{if $smarty.section.outer.index is odd by 3}
4
<
tr
bgcolor
="#EFEFEF"
><
td
>
5
{$smarty.section.outer.index}-{$smarty.section.outer.rownum} . {$FirstName[outer]} {$LastName[outer]}
6
td
>
tr
>
7
{else}
8
<
tr
bgcolor
="#FFFFFFF"
><
td
>
9
{$smarty.section.outer.index}-{$smarty.section.outer.rownum} * {$FirstName[outer]} {$LastName[outer]}
10
td
>
tr
>
11
{/if}
12
{sectionelse}
13
none
14
{/section}
15
16
table
>
test.php
1
php
2
3
require
'
lib/Smarty-2.6.11/libs/Smarty.class.php
'
;
4
5
$smarty
=
new
Smarty;
6
7
//
$smarty->compile_check = true;
8
//$smarty->debugging = true;
9
10
//$smarty->assign("hello","byby");
11
$smarty
->
assign(
"
FirstName
"
,
array
(
"
John
"
,
"
Mary
"
,
"
James
"
,
"
Henry
"
,
"
Tom
"
,
"
Hello
"
));
12
13
$smarty
->
display(
'
test.tpl
'
);
14
15
?>
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项