Smarty 기본
2.5.x: Smarty 3 4.0.x: Smarty 4
Smarty은 개발자가 애플리케이션 로직에서 프레젠테이션(HTML/CSS)을 분리할 수 있도록 하는 PHP용 템플릿 엔진입니다. XOOPS는 모든 템플릿 요구 사항에 Smarty을 사용하여 PHP 코드와 HTML 출력을 깔끔하게 분리합니다.
관련 문서
섹션 제목: “관련 문서”- 테마 개발 - XOOPS 테마 제작
- 템플릿 변수 - 템플릿에서 사용 가능한 변수
- Smarty-4-마이그레이션 - Smarty 3에서 4로 업그레이드
Smarty이란 무엇인가요?
섹션 제목: “Smarty이란 무엇인가요?”Smarty은 다음을 제공합니다.
- 관심사항 분리: 템플릿에 HTML을 유지하고 클래스에 PHP 로직을 유지합니다.
- 템플릿 상속: 간단한 블록으로 복잡한 레이아웃 구축
- 캐싱: 컴파일된 템플릿으로 성능 향상
- 수정자: 내장 또는 맞춤 기능을 사용하여 출력 변환
- 보안: PHP 함수 템플릿이 액세스할 수 있는 제어
XOOPS Smarty 구성
섹션 제목: “XOOPS Smarty 구성”XOOPS는 사용자 정의 구분 기호를 사용하여 Smarty을 구성합니다.
Default Smarty: { and }XOOPS Smarty: <{ and }>이렇게 하면 템플릿의 JavaScript 코드와의 충돌을 방지할 수 있습니다.
기본 구문
섹션 제목: “기본 구문”변수는 PHP에서 템플릿으로 전달됩니다.
// In PHP$GLOBALS['xoopsTpl']->assign('title', 'My Page Title');$GLOBALS['xoopsTpl']->assign('count', 42);{* In template *}<h1><{$title}></h1><p>Total items: <{$count}></p>어레이 액세스
섹션 제목: “어레이 액세스”// PHP$item = [ 'id' => 1, 'title' => 'Article Title', 'author' => 'John Doe'];$GLOBALS['xoopsTpl']->assign('item', $item);{* Template *}<h2><{$item.title}></h2><p>By: <{$item.author}></p>개체 속성
섹션 제목: “개체 속성”// PHP$GLOBALS['xoopsTpl']->assign('user', $xoopsUser);{* Template *}<p>Welcome, <{$user->getVar('uname')}>!</p>Smarty의 주석은 HTML로 렌더링되지 않습니다.
{* This is a comment - it will not appear in the HTML output *}
{* Multi-line comments are also supported*}제어 구조
섹션 제목: “제어 구조”If/Else 문
섹션 제목: “If/Else 문”<{if $user_logged_in}> <p>Welcome back!</p><{elseif $is_guest}> <p>Hello, Guest!</p><{else}> <p>Please log in.</p><{/if}>비교 연산자
섹션 제목: “비교 연산자”{* Equality *}<{if $status == 'published'}>Published<{/if}><{if $status eq 'published'}>Published<{/if}>
{* Inequality *}<{if $count != 0}>Has items<{/if}><{if $count neq 0}>Has items<{/if}>
{* Greater/Less than *}<{if $count > 10}>Many items<{/if}><{if $count gt 10}>Many items<{/if}><{if $count < 5}>Few items<{/if}><{if $count lt 5}>Few items<{/if}>
{* Greater/Less than or equal *}<{if $count >= 10}>Ten or more<{/if}><{if $count gte 10}>Ten or more<{/if}><{if $count <= 5}>Five or less<{/if}><{if $count lte 5}>Five or less<{/if}>
{* Logical operators *}<{if $logged_in && $is_admin}>Admin Panel<{/if}><{if $logged_in and $is_admin}>Admin Panel<{/if}><{if $option1 || $option2}>One option selected<{/if}><{if $option1 or $option2}>One option selected<{/if}><{if !$is_banned}>Access granted<{/if}><{if not $is_banned}>Access granted<{/if}>비어 있음/Isset 확인 중
섹션 제목: “비어 있음/Isset 확인 중”{* Check if variable exists and has value *}<{if $title}> <h1><{$title}></h1><{/if}>
{* Check if array is not empty *}<{if $items|@count > 0}> <ul> <{foreach $items as $item}> <li><{$item.name}></li> <{/foreach}> </ul><{/if}>
{* Using isset *}<{if isset($description)}> <p><{$description}></p><{/if}>Foreach 루프
섹션 제목: “Foreach 루프”{* Basic foreach *}<ul><{foreach $items as $item}> <li><{$item.name}></li><{/foreach}></ul>
{* With key *}<{foreach $options as $key => $value}> <option value="<{$key}>"><{$value}></option><{/foreach}>
{* With @index, @first, @last *}<{foreach $items as $item}> <{if $item@first}><ul><{/if}> <li class="item-<{$item@index}>"><{$item.name}></li> <{if $item@last}></ul><{/if}><{/foreach}>
{* Alternate row colors *}<{foreach $rows as $row}> <tr class="<{if $row@iteration is odd}>odd<{else}>even<{/if}>"> <td><{$row.name}></td> </tr><{/foreach}>
{* Foreachelse for empty arrays *}<{foreach $items as $item}> <li><{$item.name}></li><{foreachelse}> <li>No items found.</li><{/foreach}>For 루프
섹션 제목: “For 루프”<{for $i=1 to 10}> <p>Item <{$i}></p><{/for}>
<{for $i=10 to 1 step -1}> <p>Countdown: <{$i}></p><{/for}>While 루프
섹션 제목: “While 루프”<{while $count > 0}> <p><{$count}></p> <{$count = $count - 1}><{/while}>변수 수정자
섹션 제목: “변수 수정자”수정자는 변수 출력을 변환합니다.
문자열 수정자
섹션 제목: “문자열 수정자”{* HTML escape (always use for user input!) *}<{$title|escape}><{$title|escape:'html'}>
{* URL encoding *}<{$url|escape:'url'}>
{* Uppercase/Lowercase *}<{$name|upper}><{$name|lower}><{$name|capitalize}>
{* Truncate text *}<{$content|truncate:100:'...'}>
{* Strip HTML tags *}<{$html|strip_tags}>
{* Replace *}<{$text|replace:'old':'new'}>
{* Word wrap *}<{$text|wordwrap:80:"\n"}>
{* Default value *}<{$optional_var|default:'No value'}>숫자 수정자
섹션 제목: “숫자 수정자”{* Number formatting *}<{$price|string_format:"%.2f"}><{$count|number_format}>
{* Date formatting *}<{$timestamp|date_format:"%B %e, %Y"}><{$timestamp|date_format:"%Y-%m-%d %H:%M"}>배열 수정자
섹션 제목: “배열 수정자”{* Count items *}<{$items|@count}> items
{* Join array *}<{$tags|@implode:', '}>
{* JSON encode *}<{$data|@json_encode}>체인 수정자
섹션 제목: “체인 수정자”<{$content|strip_tags|truncate:200:'...'|escape}>포함 및 삽입
섹션 제목: “포함 및 삽입”다른 템플릿 포함
섹션 제목: “다른 템플릿 포함”{* Include a template file *}<{include file="db:mymodule_header.tpl"}>
{* Include with variables *}<{include file="db:mymodule_item.tpl" item=$currentItem}>
{* Include with assigned variables *}<{include file="db:sidebar.tpl" assign="sidebar_content"}><div class="sidebar"><{$sidebar_content}></div>동적 콘텐츠 삽입
섹션 제목: “동적 콘텐츠 삽입”{* Insert calls a PHP function for dynamic content *}<{insert name="getBanner"}>템플릿에 변수 할당
섹션 제목: “템플릿에 변수 할당”{* Simple assignment *}<{assign var="page_title" value="Welcome"}><{$page_title = "Welcome"}>
{* Assignment from expression *}<{assign var="full_name" value="`$first_name` `$last_name`"}>
{* Capture block content *}<{capture name="sidebar"}> <h3>Sidebar</h3> <ul> <li>Link 1</li> <li>Link 2</li> </ul><{/capture}><div class="sidebar"><{$smarty.capture.sidebar}></div>내장 Smarty 변수
섹션 제목: “내장 Smarty 변수”$smarty 변수
섹션 제목: “$smarty 변수”{* Current timestamp *}<{$smarty.now|date_format:"%Y-%m-%d"}>
{* Request variables *}<{$smarty.get.page}><{$smarty.post.username}><{$smarty.request.id}><{$smarty.cookies.session_id}><{$smarty.server.HTTP_HOST}>
{* Constants *}<{$smarty.const.XOOPS_URL}>
{* Configuration variables *}<{$smarty.config.var_name}>
{* Template info *}<{$smarty.template}><{$smarty.current_dir}>
{* Smarty version *}<{$smarty.version}>
{* Section/Foreach properties *}<{$smarty.foreach.items.index}><{$smarty.foreach.items.iteration}><{$smarty.foreach.items.first}><{$smarty.foreach.items.last}>리터럴 블록
섹션 제목: “리터럴 블록”중괄호가 있는 JavaScript의 경우:
<{literal}><script> var config = { url: 'https://example.com', count: 10 }; if (config.count > 5) { console.log('Many items'); }</script><{/literal}>또는 JavaScript 내에서 Smarty 변수를 사용하세요.
<script>var moduleUrl = '<{$xoops_url}>/modules/mymodule';var items = <{$items_json}>;</script>사용자 정의 기능
섹션 제목: “사용자 정의 기능”XOOPS는 사용자 정의 Smarty 기능을 제공합니다.
{* XOOPS Image URL *}<img src="<{xoImgUrl}>images/logo.png" alt="Logo">
{* XOOPS Module URL *}<a href="<{xoModuleUrl}>">Module Home</a>
{* App URL *}<a href="<{xoAppUrl 'item.php'}>?id=<{$item.id}>">View Item</a>모범 사례
섹션 제목: “모범 사례”항상 출력을 탈출하세요
섹션 제목: “항상 출력을 탈출하세요”{* For user-generated content, always escape *}<p><{$user_comment|escape}></p>
{* For HTML content, use appropriate method *}<div><{$content}></div> {* Only if content is pre-sanitized *}의미 있는 변수 이름을 사용하세요
섹션 제목: “의미 있는 변수 이름을 사용하세요”// Good$GLOBALS['xoopsTpl']->assign('article_title', $title);$GLOBALS['xoopsTpl']->assign('article_items', $items);
// Avoid$GLOBALS['xoopsTpl']->assign('t', $title);$GLOBALS['xoopsTpl']->assign('arr', $items);논리를 최소화하세요
섹션 제목: “논리를 최소화하세요”템플릿은 프레젠테이션에 중점을 두어야 합니다. 복잡한 논리를 PHP로 이동합니다.
{* Avoid complex logic in templates *}{* Bad *}<{if $user && $user->getVar('level') > 5 && $user->getVar('status') == 'active' && $permissions|in_array:'edit'}>
{* Good - calculate in PHP and pass a simple flag *}<{if $can_edit}>템플릿 상속 사용
섹션 제목: “템플릿 상속 사용”일관된 레이아웃을 위해 템플릿 상속을 사용하십시오(테마 개발 참조).
디버깅 템플릿
섹션 제목: “디버깅 템플릿”디버그 콘솔
섹션 제목: “디버그 콘솔”{* Show all assigned variables *}<{debug}>임시 출력
섹션 제목: “임시 출력”{* Debug specific variable *}<pre><{$variable|@print_r}></pre><pre><{$variable|@var_export}></pre>일반적인 XOOPS 템플릿 패턴
섹션 제목: “일반적인 XOOPS 템플릿 패턴”모듈 템플릿 구조
섹션 제목: “모듈 템플릿 구조”{* Module header *}<div class="mymodule"> <h2><{$module_name}></h2>
{* Breadcrumb *} <{if $breadcrumb}> <nav class="breadcrumb"> <{foreach $breadcrumb as $crumb}> <{if $crumb@last}> <span><{$crumb.title}></span> <{else}> <a href="<{$crumb.link}>"><{$crumb.title}></a> » <{/if}> <{/foreach}> </nav> <{/if}>
{* Content *} <div class="content"> <{$content}> </div></div>페이지 매김
섹션 제목: “페이지 매김”<{if $page_nav}><div class="pagination"> <{$page_nav}></div><{/if}>양식 표시
섹션 제목: “양식 표시”<{if $form}><div class="form-container"> <{$form}></div><{/if}>#smarty #템플릿 #xoops #frontend #템플릿 엔진