Posted on 2008/03/12 11:52
Filed Under Development/PHP

스마티 링크
http://www.smarty.net/
http://www.smarty.net/manual/en/

원문 : http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=8991

예전에 template_ 에 대한 정보는 잠깐 봤던 적이 있었는데 흠... 요즘에는 둘 사이가 어떤지 모르겠네요 ㅎㅎ 머가 성능이 나은지 쓰기 편한지 ㅋㅋㅋ

원문에서 소스 단위로만 정리해봤습니다. 역시 이해하는데는 예제가 짱 -_- ㅋ

1. 기본적인 변수 대입

--------- index.php --------
<?php 
require("Smarty.class.php"); 
$smarty = new Smarty; 
$smarty->assign("Name","Ned"); 
$smarty->assign(array( 
                      "FirstName" => "Ned", 
                      "LastName" => "Flanders", 
                      "Address" => "Springfield" 
                      )); 
$zipcode = "55555"; 
$smarty->assign("Zipcode",$zipcode); 
$smarty->display("index.tpl"); 
?> 



--------- templates/index.tpl --------

<HTML> 
<TITLE>Hello</TITLE> 
<BODY> 
    Hello, {$Name}!<br> 
    {$FirstName}, {$LastName}<br> 
    {$Address}, {$Zipcode} 
</BODY> 
</HTML>




2. 템플릿 포함 시키기

--------- index.php --------

<?php 
require("Smarty.class.php"); 
$smarty = new Smarty; 
$smarty->assign("Name","Ned"); 
$smarty->assign(array( 
                      "FirstName" => "Ned", 
                      "LastName" => "Flanders", 
                      "Address" => "Springfield" 
                      )); 
$zipcode = "55555"; 
$smarty->assign("Zipcode",$zipcode); 
$smarty->display("index.tpl"); 
?> 


--------- templates/index.tpl --------

{include file="header.tpl" title="Home Page"} 
    Hello, {$Name}!<br> 
    {$FirstName}, {$LastName}<br> 
    {$Address}, {$Zipcode} 
{include file="footer.tpl"} 


--------- templates/header.tpl --------

<HTML> 
<TITLE>{$title|default:"no title"}</TITLE> 
<BODY> 


--------- templates/footer.tpl --------
</BODY> 
</HTML> 



3. IF/ELSEIF/ELSE

--------- templates/index.tpl --------
{include file="header.tpl" 
title="Home Page"} 
        {if $Name eq ""} 
                Hello, 
Noname!<br> 
        {elseif $Name eq "Ned"} 
                Hello, 
Neddy!<br> 
        {else} 
                Hello, {$Name}<br> 

        {/if} 

        {$FirstName}, {$LastName}<br> 

        {$Address}, {$Zipcode} 
{include file="footer.tpl"} 



4. SECTIONS: Dynamic Blocks (섹션: 동적 블럭) - 배열들을 반복해서 출력

- 배열로 접근

--------- 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"} 


 - 배열의 키값으로 접근

--------- 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"} 


5. FOREACH(반복문)

{foreach name=outer item=contact from=$contacts} 
  {foreach key=key 
item=item from=$contact} 
    {$smarty.foreach.outer.iteration}. contact 
{$key}: {$item} 
  {/foreach} 
{foreachelse} 
  no contacts 

{/foreach} 


 - 속성
from: 반복의 대상이 되는 배열
item: 배열의 현재 요소를 가리키는 변수의 이름
key:  배열의 현재 키를 가리키는 변수의 이름(선택적)
name: foreach의 이름 (선택적)


2008/03/12 11:52 2008/03/12 11:52

트랙백 주소 : http://www.takeone.pe.kr/trackback/171

Counter

· Total
: 371794
· Today
: 37
· Yesterday
: 50