Template Smarty in XOOPS
Panoramica
Sezione intitolata “Panoramica”XOOPS utilizza il motore di template Smarty per separare la presentazione dalla logica. Questa guida copre la sintassi Smarty, le funzionalità specifiche di XOOPS e le best practice dei template.
Sintassi di base
Sezione intitolata “Sintassi di base”Variabili
Sezione intitolata “Variabili”{* Variabili scalari *}<{$variable}><{$article.title}><{$user->getUsername()}>
{* Accesso a array *}<{$items[0]}><{$config['setting']}>
{* Valori predefiniti *}<{$title|default:'Untitled'}>Modificatori
Sezione intitolata “Modificatori”{* Trasformazioni di testo *}<{$text|upper}><{$text|lower}><{$text|capitalize}><{$text|truncate:100:'...'}>
{* Gestione HTML *}<{$content|strip_tags}><{$html|escape:'html'}><{$url|escape:'url'}>
{* Formattazione della data *}<{$timestamp|date_format:'%Y-%m-%d'}><{$date|date_format:$xoops_config.dateformat}>
{* Concatenamento di modificatori *}<{$text|strip_tags|truncate:50|escape}>Condizionali
Sezione intitolata “Condizionali”{* If/else *}<{if $logged_in}> Benvenuto, <{$username}>!<{elseif $is_guest}> Per favore accedi.<{else}> Stato sconosciuto.<{/if}>
{* Confronti *}<{if $count > 0}><{if $status == 'published'}><{if $items|@count >= 5}>
{* Operatori logici *}<{if $is_admin && $can_edit}><{if $type == 'news' || $type == 'article'}><{if !$is_hidden}>{* Foreach con elementi *}<{foreach item=article from=$articles}> <h2><{$article.title}></h2><{/foreach}>
{* Con chiave *}<{foreach key=id item=value from=$items}> <{$id}>: <{$value}><{/foreach}>
{* Con info di iterazione *}<{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 per array vuoti *}<{foreach item=item from=$items}> <{$item.name}><{foreachelse}> Nessun elemento trovato.<{/foreach}>Sezioni (Legacy)
Sezione intitolata “Sezioni (Legacy)”<{section name=i loop=$items}> <{$items[i].title}><{/section}>Funzionalità specifiche di XOOPS
Sezione intitolata “Funzionalità specifiche di XOOPS”Variabili globali
Sezione intitolata “Variabili globali”{* Info del sito *}<{$xoops_sitename}><{$xoops_url}><{$xoops_rootpath}><{$xoops_theme}>
{* Info utente *}<{$xoops_isuser}><{$xoops_isadmin}><{$xoops_userid}><{$xoops_uname}>
{* Module info *}<{$xoops_dirname}><{$xoops_pagetitle}>
{* Meta *}<{$xoops_meta_keywords}><{$xoops_meta_description}>Including Files
Sezione intitolata “Including Files”{* 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"}>Block Display
Sezione intitolata “Block Display”{* In theme.html *}<{foreach item=block from=$xoops_lblocks}> <div class="block"> <{if $block.title}> <h3><{$block.title}></h3> <{/if}> <{$block.content}> </div><{/foreach}>Form Integration
Sezione intitolata “Form Integration”{* 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>Custom Functions
Sezione intitolata “Custom Functions”Registered by XOOPS
Sezione intitolata “Registered by XOOPS”{* XoopsFormLoader *}<{xoFormLoader form=$form}>
{* Breadcrumb *}<{xoBreadcrumb}>
{* Module menu *}<{xoModuleMenu}>Custom Plugins
Sezione intitolata “Custom Plugins”function smarty_function_myfunction($params, $smarty){ $name = $params['name'] ?? 'World'; return "Hello, {$name}!";}<{myfunction name="XOOPS"}>Template Organization
Sezione intitolata “Template Organization”Recommended Structure
Sezione intitolata “Recommended Structure”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.tplPartial Templates
Sezione intitolata “Partial Templates”{* 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}>Performance
Sezione intitolata “Performance”Caching
Sezione intitolata “Caching”// 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 Cache
Sezione intitolata “Clear Cache”// Clear specific template$xoopsTpl->clear_cache('mymodule_index.tpl');
// Clear all module templates$xoopsTpl->clear_all_cache();Best Practices
Sezione intitolata “Best Practices”- Escape Output - Always escape user-generated content
- Use Modifiers - Apply appropriate transformations
- Keep Logic Minimal - Complex logic belongs in PHP
- Use Partials - Reuse common template fragments
- Semantic HTML - Use proper HTML5 elements
- Accessibility - Include ARIA attributes where needed
Related Documentation
Sezione intitolata “Related Documentation”- Theme-Development - Theme creation
- ../../04-API-Reference/Template/Template-System - XOOPS template API
- ../../03-Module-Development/Block-Development - Block templates
- ../Forms/Form-Elements - Form rendering