|
|
|
루프처리메서드는 메서드 이름에 push 가 있는 것과 없는 것으로 구분할 수 있으며, push가 있는 모든 메서드는, 루프의 구간(섹션)을 발생시키는 기능을 공통으로 수행합니다. push 메서드들의 실행 회수만큼 섹션이 발생합니다.
some.tpl |
>>output |
{ @ someloop }
-- TEMPLATE SECTION --
<div>
{ someloop.variable }
</div>
{ / }
|
<div>
value 1
</div>
|
-- SECTION 1 --
|
<div>
value 2
</div>
|
-- SECTION 2 --
|
...
...
|
아래의 예제는 실행파일의 루프가 3회 돌지만 섹션이 발생하지 않아 출력되지 않은 경우입니다.
index.php |
...
$fruit=array(1=>'apple','orange','melon');
$tpl->loopLoad('fruit');
foreach ($fruit as $num => $name) {
$tpl->loopAssign(array(
'num' =>$num,
));
$tpl->loopAssign(array(
'name' =>$name,
));
}
...
|
index.tpl |
<table>
{@ fruit}
<tr><td>{ .num }</td><td>{ .name }</td></tr>
{/}
</table>
|
>>output |
<table>
</table>
|
아래의 예제는 push메서드의 중복사용으로 섹션이 6개 만들어진 경우입니다.
index.php |
...
$fruit=array(1=>'apple','orange','melon');
$tpl->loopLoad('fruit');
foreach ($fruit as $num => $name) {
$tpl->loopPushAssign(array(
'num' =>$num,
));
$tpl->loopPushAssign(array(
'name' =>$name,
));
}
...
|
>>output |
<table>
<tr><td>1</td><td></td></tr>
<tr><td></td><td>apple</td></tr>
<tr><td>2</td><td></td></tr>
<tr><td></td><td>orange</td></tr>
<tr><td>3</td><td></td></tr>
<tr><td></td><td>melon</td></tr>
</table>
|
실행파일의 루프에서, 첫 루프메서드에만 push를 넣으면 원하는 출력을 얻을 수 있습니다.
index.php |
...
$fruit=array(1=>'apple','orange','melon');
$tpl->loopLoad('fruit');
foreach ($fruit as $num => $name) {
$tpl->loopPushAssign(array(
'num' =>$num,
));
$tpl->loopAssign(array(
'name' =>$name,
));
}
...
|
>>output |
<table>
<tr><td>1</td><td>apple</td></tr>
<tr><td>2</td><td>orange</td></tr>
<tr><td>3</td><td>melon</td></tr>
</table>
|
실제작업할 때는 loopPushAssign() 메서드를 한 번만 사용하고 필요에 따라 예약변수를 활용하면, 대부분의 루프변수할당을 처리할 수 있을 것입니다.
다음은 루프메서드를 바르게 사용한 예제들입니다. push의 위치를 확인해 보시기 바랍니다.
index1.php |
...
$tpl->loopLoad('abc');
while (...) {
$tpl->loopPushAssign(array(;
...
));
...
$tpl->loopAssign(array(
...
...
));
}
...
|
index2.php |
...
$tpl->loopLoad('abc', 1);
while (...) {
$tpl->loopPushLoad('xyz', 2);
while (...) {
$tpl->loopPushAssign(array(
...
), 2);
}
$tpl->loopAssign(array(
...
...
), 1);
}
...
|
index3.php |
...
$tpl->loopLoad('aaa', 1);
while (...) {
$tpl->loopPushAssign(array(;
...
...
));
$tpl->loopLoad('bbb', 2);
while (...) {
$tpl->loopPushLoad('ccc', 3);
while (...) {
$tpl->loopPushAssign(array(
...
...
), 3);
}
$tpl->loopAssign(array(
...
), 2);
}
$tpl->loopAssign(array(
...
...
), 1);
}
...
|
index4.php |
...
$tpl->loopLoad('aaa', 1);
while (...) {
$tpl->loopPushLoad('xxx', 2);
while (...) {
$tpl->loopPushAssign(array(
...
...
), 2);
}
$tpl->loopLoad('yyy', 2);
while (...) {
$tpl->loopPushAssign(array(
...
...
), 2);
}
$tpl->loopLoad('zzz', 2);
while (...) {
$tpl->loopPushAssign(array(
...
...
), 2);
}
}
...
|
|
|