[필독] 템플릿 활용기초
목차
  1. 템플릿?
  2. 템플릿 태그
  3. 템플릿 변수 출력
  4. 루프(1)
  5. 루프(2) - 예약변수
  6. 분기 - if
  7. 하위템플릿 인클루드
  8. 주석
  1. 템플릿?

    프로그램과 디자인을 분리하여 동적데이타 처리를 위한 간결한 인터페이스를 제공합니다.
  2. 템플릿 태그

    템플릿 태그는 템플릿 파일내에서 템플릿엔진이 해석할 위치를 표시합니다.
    해석할 내용이 변수이든 제어명령이든 관계없이 { } , <!--{ } , { }--> , <!--{ }--> 모두가 유효한 템플릿 태그입니다.
  3. 템플릿 변수 출력

    명령어 의미 사용법
    = 템플릿변수의 값을 출력 {= template_variable }
    템플릿태그 내에서 함수를 사용하지 않을 때는 {=title} 대신 {title} 과 같이 출력명령어를 생략할 수 있습니다.
    예제
    index.php
    <?php
    $tpl->assign(array(
        'title'  =>'First Template_',
        'content'=>'Fill me in',
    ));
    ?>
    index.htm
    <html>
    <head>
    <title>{title}</title>
    </head>
    <body>
    {content}
    </body>
    </html>
    >>output
    <html>
    <head>
    <title>First Template_</title>
    </head>
    <body>
    Fill me in
    </body>
    </html>
  4. 루프(1)

    명령어 의미 사용법
    @ 루프의 시작 {@ loop_id}
    : 루프가 돌지 않았을 때 출력 (옵션) {:}
    / 루프의 끝 {/}
    기본 루프 예제
    index.php
    <?php
    $loop = array(
        array(
            'name' =>'banana',
            'color'=>'yellow',
        ),
        array(
            'name' =>'apple',
            'color'=>'red',
        ),
    );
    $tpl->assign('list', $loop);
    ?>
    index.htm
    <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>
    중첩 루프 예제
    index.php
    <?php
    $loop1 = array(
        array(
            'name' =>'sport',
            'item' => array(
                array(
                    'name'=>'pingpong',
                    'num' =>5,
                ),
                array(
                    'name'=>'running',
                    'num' =>16,
                ),
            ),
        ),
        array(
            'name' =>'book',
            //'item' => null
        ),
    );
    $tpl->assign('group', $loop1);
    ?>
    index.htm
    <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>

    </table>
    최상위루프의 변수이면 { .name }, 중첩루프의 변수면 { ..name } 과 같이 루프 아이디를 생략하고 루프변수를 짧게 표시할 수 있습니다. 이 때, 점의 개수는 중첩의 깊이를 나타냅니다.
    루프 아이디 생략 예제
    index.htm
    <table>
    <!--{@ group }-->
    <tr>
        <td> group: {.name}</td>
        <td> item:
        <!--{@ item}-->
            {.name}-{..name}({..num})
        <!--{:}-->
            -- if no item, print this --
        <!--{/}-->
        </td>
    </tr>
    <!--{/}-->

    </table>
  5. 루프(2)

    ※  index_ size_

    예약변수 설명
    index_ 0 부터 시작, 루프가 반복할 때 1 씩 증가
    size_ 루프의 전체 반복 회수
    index_ size_ 예제
    index.php
    <?php
    $loop = array(
        array(
            'title' =>'I Wish..',
        ),
        array(
            'title' =>'Love Revolution 21',
        ),
        array(
            'title' =>'The Peace',
        ),
    );
    $tpl->assign('list',  $loop);
    $tpl->assign('start', 10);
    ?>
    index.tpl
    <div>num of list: { list.size_ }</div>
    <!--{@ list}-->
    <div>{ list.index_ + start }th. { list.title }</div>
    <!--{/}-->
    >>output
    <div>num of list: 3</div>
    <div>10th. I Wish..</div>
    <div>11th. Love Revolution 21</div>
    <div>12th. The Peace</div>
    size_ 는 해당루프 밖에서도 사용할 수 있습니다. 해당루프 밖에서 사용할 때는 {.size_} 처럼 루프아이디를 생략할 수는 없습니다.

    ※  key_ value_

    예약변수 설명
    key_ 루프로 할당된 배열의 키
    value_ 루프로 할당된 배열의 값
    key_ value_ 예제
    index.php
    <?php
    $loop = array('apple'=>'red', 'banana'=>'yellow', 30 =>'unknown');
    $tpl->assign('fruit', $loop);
    ?>
    index.tpl
    <!--{@ fruit}-->
    <div>{fruit.index_} - {fruit.key_} - {fruit.value_}</div>
    <!--{/}-->

    <div>num of fruit: {fruit.size_}</div>
    >>output
    <div>0 - apple - red</div>
    <div>1 - banana - yellow</div>
    <div>2 - 30 - unknown</div>

    <div>num of list: 3</div>
  6. 분기 - if

    명령어 의미 사용법
    ? if {? expression }
    : elseif, else (옵션) {: expression }, {:}
    / endif {/}
    문맥에 의해, ':' 은 분기의 else if, else 또는 루프의 else 로, '/' 은 루프나 분기의 종결문으로 해석됩니다.
    분기 예제
    index.php
    <?php
    $tpl->assign('fruit', 'banana');
    ?>
    index.tpl
    <!--{? fruit=='apple' || person=='santa' }-->
    <div> red </div>
    <!--{: fruit=='banana' }-->
    <div> yellow </div>
    <!--{: fruit=='orange' }-->
    <div> orange </div>
    <!--{:}-->
    <div> unknown </div>
    <!--{/}-->
    >> output
    <div> yellow </div>
    루프-분기 중첩 예제
    index.php
    <?php
    $page_loop = array(
        1=>'page1.php',
        2=>'page2.php',
        3=>'page3.php',
    );
    $tpl->assign(array(
        'current'=>2,
        'page'   =>$page_loop,
    ));
    ?>
    index.tpl
    <!--{@ page}-->
        <!--{? page.key_ == current }-->
            [ { page.key_ } ]
        <!--{:}-->
            <a href='{ page.value_ }'>[ { page.key_ } ]</a>
        <!--{/}-->
    <!--{:}-->
        -- no page --
    <!--{/}-->
    >>output
            <a href='page1.php'>[ 1 ]</a>
            [ 2 ]
            <a href='page3.php'>[ 3 ]</a>
    분기와 분기, 분기와 루프 역시 자유롭게 중첩하여 사용할 수 있습니다.
  7. 하위템플릿 인클루드

    명령어 의미 사용법
    # 하위템플릿파일 인클루드 {# file_id }
    예제
    directory
    /
    ┗━ home
       ┗━ htdocs
          ┣━ index.php
          ┣━ _template
          ┣━ ┣━ layout.htm
          ┣━ ┣━ left.htm
          ┣━ ┗━ intro
          ┣━    ┗━ intro.htm
          ┗━ _compile
              ┗━ ...
    index.php
    <?php
    $tpl->define(array(
        'body' => 'body.htm',
        'left' => 'left.htm',
        'main' => 'intro/intro.htm',
    ));
    $tpl->assign(array(
        'link'  => 'xtac.net',
        'text1' => 'fine',
        'text2' => 'thanks',
    ));
    $tpl->print_('body');
    ?>
    body.htm
    <table>
    <tr><td> {# left} </td><td> {# main} </td></tr>
    </table>
    left.htm
    { link }
    intro/intro.htm
    <div>{ text1 }..{ text2 }</div>
    >>output
    <table>
    <tr><td> xtac.net </td><td> <div>fine..thanks</div> </td></tr>
    </table>
  8. 주석

    ※  꼬리말형 주석

    명령어 의미 사용법
    // 변환시 태그내 '//' 이하문자 삭제 { any_template_code // any_string }
    템플릿코드가 유효하지 않거나 이스케이프되었을 때는 삭제되지 않습니다.
    꼬리말형 주석 예제
    index.tpl
    <table>
    <!--{@ list // loop starts!! }-->
    <tr>
        <td>{start - .index_ // list number }</td>
        <td>{.title}</td>
    </tr>
    <!--{/// list ends!! }-->
    </table>

    ※  블럭형 주석

    명령어 의미 사용법
    * 변환파일에서 삭제됨 {* any_string *}
    블럭형 주석 예제
    index.tpl
    <!--{* Template {comments} are {removed} in compiled file *}-->
    <table>
    <tr>
        <td width="50px">
            <!--{ template * expression / overflows + width50px }-->{*no*}
        </td>
        <td width="250px">
            { width250px & is | enough }
        </td>
    </tr>
    </table>
    {* *} , <!--{* *} , {* *}--> , <!--{* *}--> 모두 유효한 템플릿 주석입니다.