Smarty XOOPS의 템플릿
XOOPS는 프레젠테이션을 로직에서 분리하기 위해 Smarty 템플릿 엔진을 사용합니다. 이 가이드에서는 Smarty 구문, XOOPS 관련 기능 및 템플릿 모범 사례를 다룹니다.
기본 구문
섹션 제목: “기본 구문”{* Scalar variables *}<{$variable}><{$article.title}><{$user->getUsername()}>
{* Array access *}<{$items[0]}><{$config['setting']}>
{* Default values *}<{$title|default:'Untitled'}>수정자
섹션 제목: “수정자”{* Text transformations *}<{$text|upper}><{$text|lower}><{$text|capitalize}><{$text|truncate:100:'...'}>
{* HTML handling *}<{$content|strip_tags}><{$html|escape:'html'}><{$url|escape:'url'}>
{* Date formatting *}<{$timestamp|date_format:'%Y-%m-%d'}><{$date|date_format:$xoops_config.dateformat}>
{* Chaining modifiers *}<{$text|strip_tags|truncate:50|escape}>조건부
섹션 제목: “조건부”{* If/else *}<{if $logged_in}> Welcome, <{$username}>!<{elseif $is_guest}> Please log in.<{else}> Unknown state.<{/if}>
{* Comparisons *}<{if $count > 0}><{if $status == 'published'}><{if $items|@count >= 5}>
{* Logical operators *}<{if $is_admin && $can_edit}><{if $type == 'news' || $type == 'article'}><{if !$is_hidden}>{* Foreach with items *}<{foreach item=article from=$articles}> <h2><{$article.title}></h2><{/foreach}>
{* With key *}<{foreach key=id item=value from=$items}> <{$id}>: <{$value}><{/foreach}>
{* With iteration info *}<{foreach item=item from=$items name=itemloop}> <{$smarty.foreach.itemloop.index}> <{$smarty.foreach.itemloop.iteration}> <{$smarty.foreach.itemloop.first}> <{$smarty.foreach.itemloop.last}><{/foreach}>
{* Foreachelse for empty arrays *}<{foreach item=item from=$items}> <{$item.name}><{foreachelse}> No items found.<{/foreach}>섹션(레거시)
섹션 제목: “섹션(레거시)”<{section name=i loop=$items}> <{$items[i].title}><{/section}>XOOPS 관련 기능
섹션 제목: “XOOPS 관련 기능”전역 변수
섹션 제목: “전역 변수”{* Site info *}<{$xoops_sitename}><{$xoops_url}><{$xoops_rootpath}><{$xoops_theme}>
{* User info *}<{$xoops_isuser}><{$xoops_isadmin}><{$xoops_userid}><{$xoops_uname}>
{* Module info *}<{$xoops_dirname}><{$xoops_pagetitle}>
{* Meta *}<{$xoops_meta_keywords}><{$xoops_meta_description}>파일 포함
섹션 제목: “파일 포함”{* Include from theme *}<{include file="theme:header.html"}>
{* Include from module *}<{include file="db:modulename_partial.tpl"}>
{* Include with variables *}<{include file="db:mymodule_item.tpl" item=$article}>
{* Include from file system *}<{include file="$xoops_rootpath/modules/mymodule/templates/partial.tpl"}>블록 표시
섹션 제목: “블록 표시”{* In theme.html *}<{foreach item=block from=$xoops_lblocks}> <div class="block"> <{if $block.title}> <h3><{$block.title}></h3> <{/if}> <{$block.content}> </div><{/foreach}>양식 통합
섹션 제목: “양식 통합”{* XoopsForm rendering *}<{$form.javascript}><form action="<{$form.action}>" method="<{$form.method}>"> <{foreach item=element from=$form.elements}> <div class="form-group"> <label><{$element.caption}></label> <{$element.body}> <{if $element.description}> <small><{$element.description}></small> <{/if}> </div> <{/foreach}></form>사용자 정의 기능
섹션 제목: “사용자 정의 기능”XOOPS에 등록됨
섹션 제목: “XOOPS에 등록됨”{* XoopsFormLoader *}<{xoFormLoader form=$form}>
{* Breadcrumb *}<{xoBreadcrumb}>
{* Module menu *}<{xoModuleMenu}>사용자 정의 플러그인
섹션 제목: “사용자 정의 플러그인”function smarty_function_myfunction($params, $smarty){ $name = $params['name'] ?? 'World'; return "Hello, {$name}!";}<{myfunction name="XOOPS"}>템플릿 구성
섹션 제목: “템플릿 구성”권장 구조
섹션 제목: “권장 구조”templates/├── admin/│ ├── index.tpl│ ├── item_list.tpl│ └── item_form.tpl├── blocks/│ ├── recent.tpl│ └── popular.tpl├── frontend/│ ├── index.tpl│ ├── item_view.tpl│ └── item_list.tpl└── partials/ ├── _header.tpl ├── _footer.tpl └── _pagination.tpl부분 템플릿
섹션 제목: “부분 템플릿”{* partials/_pagination.tpl *}<nav class="pagination"> <{if $page > 1}> <a href="<{$base_url}>&page=<{$page-1}>">Previous</a> <{/if}>
<span>Page <{$page}> of <{$total_pages}></span>
<{if $page < $total_pages}> <a href="<{$base_url}>&page=<{$page+1}>">Next</a> <{/if}></nav>
{* Usage *}<{include file="db:mymodule_pagination.tpl" page=$current_page total_pages=$pages base_url=$url}>// In PHP$xoopsTpl->caching = 1;$xoopsTpl->cache_lifetime = 3600; // 1 hour
// Check if cachedif (!$xoopsTpl->is_cached('mymodule_index.tpl')) { // Fetch data only if not cached $items = $handler->getObjects(); $xoopsTpl->assign('items', $items);}캐시 지우기
섹션 제목: “캐시 지우기”// Clear specific template$xoopsTpl->clear_cache('mymodule_index.tpl');
// Clear all module templates$xoopsTpl->clear_all_cache();모범 사례
섹션 제목: “모범 사례”- 이스케이프 출력 - 항상 사용자 생성 콘텐츠를 이스케이프합니다.
- 수정자 사용 - 적절한 변환 적용
- 로직을 최소화하세요 - 복잡한 로직은 PHP에 속합니다
- 부분 사용 - 공통 템플릿 조각 재사용
- 의미론적 HTML - 적절한 HTML5 요소 사용
- 접근성 - 필요한 경우 ARIA 속성을 포함합니다.
관련 문서
섹션 제목: “관련 문서”- 테마 개발 - 테마 제작 -../../04-API-참조/템플릿/템플릿-시스템 - XOOPS 템플릿 API -../../03-모듈-개발/블록-개발 - 블록 템플릿 -../Forms/Form-Elements - 양식 렌더링