'smarty 세션'에 해당되는 글 1

  1. 2009.12.23 Smarty 섹션: 동적 블럭(SECTIONS: Dynamic Blocks)
SECTIONS: Dynamic Blocks (섹션: 동적 블럭)
------------------------------------------    

Smarty에서 {section} 기능을 사용하면 비교적 간단하게 배열들을 반복해서 출력할 수 있
어요. 여기서 section에는 두가지 속성이 필요로 한답니다. 하나는 name인데 말 그대로
이것은 section의 이름이랍니다[역주: 아래 예제를 보시면 아시겠지만, 이 name 속성은
템플릿 내에서 사용됩니다]. 그리고 또 다른 하나는 loop라는 속성인데, 이 loop는
section에서 몇 번이나 반복할 지 반복횟수를 설명하고 있는 배열의 이름을 가리킨답니
다[역주: 아래 예제에서 "FirstName"과 "LastName"이라는 변수와 함께 지정된 배열의 크
기가 3이므로 3번 반복하게 됩니다.]


--------- index.php --------
<?php
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->assign("FirstName",array("Ned","Bart","Montgomery"));
$smarty->assign("LastName",array("Flanders","Simpson","Burns"));
$smarty->display("index.tpl");
?>

--------- templates/index.tpl --------
{include file="header.tpl" title="Home Page"}
{section name=people loop=$FirstName}
{$smarty.section.people.rownum} {$FirstName[people]} {$LastName[people]}<br>
{sectionelse}
There are no values to loop through.
{/section}
<p>
There were {$smarty.section.people.loop} names in this list.
{include file="footer.tpl"}


여기서 템플릿 내에서 사용된 {$smarty}라는 변수에 대해서 소개할 필요가 있을 것 같네
요. section 내에서 출력되는 변수들을 주목해 보세요. 이처럼 section 내에서 출력할 변
수들은 모두 반드시 섹션 이름으로 참조될 필요가 있음을 잊지 마세요. 이것은 당신이 출
력하고 싶은 값을 배열에서 현재 반복 횟수를 가지고 찾기 위해 필요한 거에요. 또한 내
부 템플릿 변수들을 가지고 현재 반복 중인 section의 총 반복횟수와 현재 반복 횟수 같
은 것도 출력이 가능하답니다. {sectoinelse}도 중요한 건데, 이것은 만일 반복할 배열인
$FirstName이 빈 배열의 경우 대신 출력되는 부분이 된답니다.

그리고 아래 예제에서 사용된 문법처럼 배열의 키들을 가지고 접근할 수도 있어요.


--------- index.php --------
<?php
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->assign(array("ContactInfo" =>
         array(
             array("FirstName" => "Ned","LastName" => "Flanders"),
             array("FirstName" => "Monty","LastName" => "Burns")
             )
));
$smarty->display("index.tpl");
?>


--------- templates/index.tpl --------
{include file="header.tpl" title="Home Page"}
{section name=people loop=$ContactInfo}
{$ContactInfo[people].FirstName}
{$ContactInfo[people].LastName}<br>
{sectionelse}
There are no values to loop through.
{/section}
<p>
There were {$smarty.section.people.loop} names in this list.
{include file="footer.tpl"}


혹은 아래 예제처럼 당신은 복합 중첩 section 문을 시도해볼 수도 있답니다.


--------- index.php --------
<?php
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->assign("FirstName",array("Ned","Bart","Montgomery"));
$smarty->assign("LastName",array("Flanders","Simpson","Burns"));
$smarty->assign("ContactNames",
        array(
         array("email","home","cell"),
         array("email","home"),
         array("email","home","fax")
         ));
$smarty->assign("ContactVals",
        array(
         array("ned@simpsons.com","555-666-7777","555-444-3333"),
         array("bart@simpsons.com","555-111-2222"),
         array("monty@simpsons.com","555-888-9999","555-234-5678"),
         ));

$smarty->display("index.tpl");
?>


--------- templates/index.tpl --------
{include file="header.tpl" title="Home Page"}
{section name=people loop=$FirstName}
{$smarty.section.people.rownum} {$FirstName[people]} {$LastName[people]}<br>
{section name=contacts loop=$ContactNames[people]}
{* for fun, lets bold every other row *}
{if $smarty.section.contacts.rownum is even}<b>{/if}
{$ContactNames[people][contacts]}: {$ContactVals[people][contacts]}<br>
{if $smarty.section.contacts.rownum is even}</b>{/if}
{/section}
<br>
{sectionelse}
There are no values to loop through.
{/section}
<p>
There were {$smarty.section.people.loop} names in this list.
{include file="footer.tpl"}


출처 : PHP SCHOOL
역자 : 조한석 님 <sizer@systong.co.kr>

'프로그래밍... > Smarty' 카테고리의 다른 글

Smarty 한글 매뉴얼  (0) 2009.12.23
Smarty 반복문(FOREACH)  (0) 2009.12.23
Smarty IF/ELSEIF/ELSE  (0) 2009.12.23
Smarty 템플릿 포함하기(INCLUDE)  (0) 2009.12.23
Smarty 변수대입(ASSIGNING VARIABLES)  (0) 2009.12.23