I. BASIC
¸ñÂ÷
1.  È¯°æ¼³Á¤
ÅÛÇø´¾ð´õ¹Ù´Â PHP 4.1.0 À̻󿡼­ ÀÛµ¿ÇÕ´Ï´Ù.
var $template_dir = '_template';  
var $compile_dir  = '_compile';
$compile_dir Àº ÅÛÇø´¾ð´õ¹Ù°¡ ÅÛÇø´ÆÄÀÏÀ» º¯È¯ÇÏ¿© ÀúÀåÇÏ´Â µð·ºÅ丮ÀÔ´Ï´Ù. ¿î¿µÃ¼Á¦»óÀÇ Àý´ë°æ·Î³ª ½ÇÇàÆÄÀÏÀ» ±âÁØÀ¸·Î ÇÑ »ó´ë°æ·Î¸¦ ÁöÁ¤ÇÕ´Ï´Ù.
/
home
htdocs
index.php (½ÇÇàÆÄÀÏ)
_template
index.tpl (ÅÛÇø´ÆÄÀÏ)
_compile
index.tpl.php (ÅÛÇø´º¯È¯ÆÄÀÏ)
¸®´ª½º¸¦ ÀÛ¾÷¿ë ¼­¹ö·Î »ç¿ëÇÒ ¶§, º¯È¯ÆÄÀÏÀÌ À§Ä¡ÇÒ µð·ºÅ丮¿¡ ´ëÇØ À¥¼­¹ö(nobody)°¡ ÀÐ°í ¾²±â ±ÇÇÑÀ» °¡Á®¾ß ÇÕ´Ï´Ù. ·¹ÆÛ·±½ºÀÇ È¯°æ¼³Á¤2 ¿¡¼­ ½ÎÀÌÆ® ±¸¼º ¹æ¹ýÀ» Âü°íÇϽñ⠹ٶø´Ï´Ù.
2.  ÅÛÇø´ ű×
ÅÛÇø´ ű״ ÅÛÇø´ ÆÄÀϳ»¿¡¼­ ÅÛÇø´¿£ÁøÀÌ ÇØ¼®ÇÒ À§Ä¡¸¦ Ç¥½ÃÇÕ´Ï´Ù. ÇØ¼®ÇÒ ³»¿ëÀÌ º¯¼öÀ̵ç Á¦¾î¸í·ÉÀÌµç °ü°è¾øÀÌ { } , <!--{ } , { }--> , <!--{ }--> ¸ðµÎ°¡ À¯È¿ÇÑ ÅÛÇø´ ű×ÀÔ´Ï´Ù.
ÅÛÇø´ ÆÄÀÏÀ» ºê¶ó¿ìÀú³ª À§ÁöÀ¨ ¿¡µðÅÍ·Î ¿­¾úÀ» ¶§, ¹Ù¸£°Ô º¸ÀÏ ¼ö ÀÖµµ·Ï Æí¸®ÇÑ´ë·Î ÀÚÀ¯·Ó°Ô ¼±ÅÃÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
3.  ÅÛÇø´ ÆÄÀÏ Á¤ÀÇ, ÅÛÇø´ º¯¼ö ÇÒ´ç, Ãâ·Â
¸Þ¼­µå ±â´É
define() »ç¿ëÇÒ ÅÛÇø´ ÆÄÀÏÀÇ ¾ÆÀ̵𸦠Á¤ÀÇ
assign() ÅÛÇø´ º¯¼ö¿¡ °ªÀ» ÇÒ´ç
print_() Ãâ·Â
¸í·É¾î ÀÇ¹Ì »ç¿ë¹ý
= ÅÛÇø´º¯¼öÀÇ °ªÀ» Ãâ·Â {= template_variable }
ÅÛÇø´ÅÂ±× ³»¿¡¼­ ÇÔ¼ö¸¦ »ç¿ëÇÏÁö ¾ÊÀ» ¶§´Â {=title} ´ë½Å {title} °ú °°ÀÌ Ãâ·Â¸í·É¾î¸¦ »ý·«ÇÒ ¼ö ÀÖ½À´Ï´Ù.
¿¹Á¦ 
index.php
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$tpl->assign(array(
    'title'  =>'First Template_',
    'content'=>'Fill me in',
));
$tpl->print_('index');
?>
index.tpl
<html>
<head>
<title>{title}</title>
</head>
<body>
{content}
</body>
</html>
>>output
<html>
<head>
<title>First Template_</title>
</head>
<body>
Fill me in
</body>
</html>
ÅÛÇø´º¯¼öÀÇ ³×ÀÌ¹Ö ±ÔÄ¢Àº ÀÚ¹Ù½ºÅ©¸³Æ®¿Í °°°í ÀÚ¹Ù½ºÅ©¸³Æ®Ã³·³ Çѱ۵µ °¡´ÉÇÕ´Ï´Ù. ´Ü '$' ´Â »ç¿ëÇÒ ¼ö ¾øÀ¸¸ç, '_' ·Î ½ÃÀÛÇÏ´Â ÅÛÇø´º¯¼ö´Â º°µµÀÇ ¿ëµµ¸¦ °¡Áý´Ï´Ù.
ÅÛÇø´ÆÄÀÏÀ» define() ¸Þ¼­µå·Î Á¤ÀÇÇÒ ¶§, $template_dir ¼Ó¼ºÀ¸·ÎºÎÅÍÀÇ »ó´ë°æ·Î¸¦ ÁöÁ¤ÇÕ´Ï´Ù.
Note:
¿£Áø ³»ºÎÀûÀ¸·Î, ÅÛÇø´ÆÄÀÏ(index.tpl)À» ¼öÁ¤ÇßÀ» ¶§¸¸ ÆÐŰÁöÀÇ º¯È¯±âÆÄÀÏ(Template_.compiler.php)À» ÀÎŬ·çµåÇϰí, º¯È¯ÆÄÀÏ(index.tpl.php)À» °»½ÅÇÕ´Ï´Ù.
4.  ·çÇÁ(1)
¸í·É¾î ÀÇ¹Ì »ç¿ë¹ý
@ ·çÇÁÀÇ ½ÃÀÛ {@ loop_id}
: ·çÇÁ°¡ µ¹Áö ¾Ê¾ÒÀ» ¶§ Ãâ·Â (¿É¼Ç) {:}
/ ·çÇÁÀÇ ³¡ {/}
±âº» ·çÇÁ ¿¹Á¦ 
index.php
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$tpl->assign('title', 'power of 2');
$loop = array();
for ($i=1; $i<=3; $i++) {
    $loop[] = array(             // or
        'exponent'=>$i,          // $loop[$i]['exponent']=$i;
        'power'   =>pow(2, $i),  // $loop[$i]['power']   =pow(2, $i);
    );                           //
}
$tpl->assign('row', $loop);
$tpl->print_('index');
?>
index.tpl
<table>
<tr><td colspan=2> {title} </td></tr>
<tr><td> exponent </td><td> 2^exponent </td></tr>
<!--{@ row}-->
<tr><td> {row.exponent} </td><td> {row.power} </td></tr>

<!--{/}-->
</table>
>>output
<table>
<tr><td colspan=2> power of 2 </td></tr>
<tr><td> exponent </td><td> 2^exponent </td></tr>
<tr><td> 1 </td><td> 2 </td></tr>
<tr><td> 2 </td><td> 4 </td></tr>
<tr><td> 3 </td><td> 8 </td></tr>

</table>
·çÇÁ·Î ÇÒ´çÇÒ ¹è¿­À» Á÷Á¢ ÀÛ¼ºÇÑ´Ù¸é ¾Æ·¡¿Í °°Àº ÇüŰ¡ µË´Ï´Ù.
index.php
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$loop = array(
    array(
        'name' =>'banana',
        'color'=>'yellow',
    ),
    array(
        'name' =>'apple',
        'color'=>'red',
    ),
);
$tpl->assign('list', $loop);
$tpl->print_('index');
?>
index.tpl
<table>
<!--{@ list}-->
<tr><td>{list.name}</td><td>{list.color}</td></tr>

<!--{/}-->
</table>
>>output
<table>
<tr><td>banana</td><td>yellow</td></tr>
<tr><td>apple</td><td>red</td></tr>

</table>
Note:
ºÎÇǰ¡ Å« ¹è¿­À» ÇÒ´çÇÏ´õ¶óµµ, PHPÀÇ ÂüÁ¶Ä«¿îÆ® ½Ã½ºÅÛÀ¸·Î ÀÎÇØ, º°µµÀÇ ¸Þ¸ð¸® °ø°£À» ¸¸µé¾î¼­ º¹»çÇÏÁö ¾ÊÀ¸¹Ç·Î ¼º´É»óÀÇ ¹®Á¦¸¦ ÀÏÀ¸Å°Áö ¾Ê½À´Ï´Ù.
Áßø ·çÇÁ ¿¹Á¦ 
index.php
<?php
...
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$group = array('sport', 'book', 'fruit');
$loop1 = array();
for ($i=0,$s=count($group); $i<$s; $i++) {
    $loop1[] = array(                 // or
        'name'=>$group[$i],           // $loop1[$i]['name']=$group[$i];
    );
    $loop2 = &$loop1[$i]['item'];     // php notice safe, valid syntax.
    $result = mysql_query('select name, num from '.$group[$i]);
    while ($data=mysql_fetch_array($result)) {
        $loop2[] = array(             // or
            'name'=>$data['name'],    // $loop2[] = $data;
            'num' =>$data['num'],
        );
    }
}
$tpl->assign('group', $loop1);
$tpl->print_('index');
?>
index.tpl
<table>
<!--{@ group }-->
<tr>
    <td> group: { group.name }</td>
    <td> item:
    <!--{@ item}-->
        {group.name}-{item.name}({item.num})
    <!--{:}-->
        -- if no item, print this --
    <!--{/}-->
    </td>
</tr>
<!--{/}-->

</table>
>>output
<table>
<tr>
    <td> group: sport</td>
    <td> item:
        sport-pingpong(5)
        sport-running(16)
    </td>
</tr>
<tr>
    <td> group: book</td>
    <td> item:
        -- if no item, print this --
    </td>
</tr>
<tr>
    <td> group: fruit</td>
    <td> item:
        fruit-apple(63)
    </td>
</tr>

</table>
¾Æ·¡ÀÇ ¹è¿­À» Á÷Á¢ ÇÒ´çÇØµµ Ãâ·Â°á°ú´Â °°½À´Ï´Ù.
Array of nested loop
$loop1 = array(
    array(
        'name' =>'sport',
        'item' => array(
            array(
                'name'=>'pingpong',
                'num' =>5,
            ),
            array(
                'name'=>'running',
                'num' =>16,
            ),
        ),
    ),
    array(
        'name' =>'book',
        //'item' => null
    ),
    array(
        'name' =>'fruit',
        'item' => array(
            array(
                'name'=>'apple',
                'num' =>63,
            ),
        ),
    ),
);
Note:
Áßø·çÇÁ¸¦ ¼³¸íÇϱâ À§ÇÑ ¿¹Á¦À̸ç, À§¿Í °°Àº ¹Ýº¹ÀûÀÎ DB Äõ¸®´Â ¹Ù¶÷Á÷ÇÏÁö ¾Ê½À´Ï´Ù.
ÅÛÇø´Å±׳»¿¡ °ø¹éÀº ÀÚÀ¯·Ó°Ô »ðÀÔÇÒ ¼ö ÀÖ½À´Ï´Ù. »óÀ§·çÇÁÀÇ º¯¼ö´Â Ãß°¡ÀûÀÎ ÇÒ´ç¾øÀÌ Áßø·çÇÁ³»¿¡¼­µµ »ç¿ëÇÒ ¼ö ÀÖÀ¸¸ç ÀϹÝÅÛÇø´º¯¼ö´Â ÅÛÇø´ Àüü¿¡¼­ À¯È¿ÇÕ´Ï´Ù.
ÃÖ»óÀ§·çÇÁÀÇ º¯¼öÀ̸é { .name }, Áßø·çÇÁÀÇ º¯¼ö¸é { ..name } °ú °°ÀÌ ·çÇÁ ¾ÆÀ̵𸦠»ý·«ÇÏ°í ·çÇÁº¯¼ö¸¦ ª°Ô Ç¥½ÃÇÒ ¼ö ÀÖ½À´Ï´Ù. ÀÌ ¶§, Á¡ÀÇ °³¼ö´Â ÁßøÀÇ ±íÀ̸¦ ³ªÅ¸³À´Ï´Ù.
<table>
<!--{@ group }-->
<tr>
    <td> group: {.name}</td>
    <td> item:
    <!--{@ item}-->
        {.name}-{..name}({..num})
    <!--{:}-->
        -- if no item, print this --
    <!--{/}-->
    </td>
</tr>
<!--{/}-->

</table>
5.  ÇÏÀ§ÅÛÇø´ ÀÎŬ·çµå
¸í·É¾î ÀÇ¹Ì »ç¿ë¹ý
# ÇÏÀ§ÅÛÇø´ÆÄÀÏ ÀÎŬ·çµå {# file_id }
¿¹Á¦ 
directory
/
home
htdocs
index.php
_template
layout.tpl
left.tpl
intro
intro.tpl
_compile
...
index.php
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define(array(
    'body' => 'body.tpl',
    'left' => 'left.tpl',
    'main' => 'intro/intro.tpl',
));
$tpl->assign(array(
    'link'  => 'xtac.net',
    'text1' => 'fine',
    'text2' => 'thanks',
));
$tpl->print_('body');
?>
body.tpl
<table>
<tr><td> {# left} </td><td> {# main} </td></tr>
</table>
left.tpl
{ link }
intro/intro.tpl
<div>{ text1 }..{ text2 }</div>
>>output
<table>
<tr><td> xtac.net </td><td> <div>fine..thanks</div> </td></tr>
</table>
ÇÏÀ§ ÅÛÇø´ÆÄÀÏ ³»¿¡µµ ÀÌ ¸í·É¾î¸¦ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. ÅÛÇø´ º¯¼ö°¡ ¾ø´Â Á¤ÀûÅØ½ºÆ®³ª PHP ÆÄÀÏÀ» ÀÎŬ·çµåÇÏ´Â ¹æ¹ýÀº ·¹ÆÛ·±½ºÀÇ define() ¸Þ¼­µå¸¦ Âü°íÇϽñ⠹ٶø´Ï´Ù.
¶ÇÇÑ ÇÑ ÆäÀÌÁö¿¡ print_() ¸Þ¼­µå¸¦ ¿©·¯ ¹ø »ç¿ëÇØ¼­ Ãâ·ÂÇØµµ ÁÁ½À´Ï´Ù.
$tpl->print_('head');
flush();
// logic for body

$tpl->print_('body');
 
2003-03-03 ~