Zum Inhalt springen

Smarty Template API-Referenz

Vollständige API-Dokumentation für Smarty-Vorlagenerstellung in XOOPS.


graph TB
subgraph "Template-Verarbeitung"
A[Controller] --> B[Variablen zuweisen]
B --> C[XoopsTpl]
C --> D[Smarty-Engine]
D --> E{Template gecacht?}
E -->|Ja| F[Aus Cache laden]
E -->|Nein| G[Template kompilieren]
G --> H[PHP ausführen]
F --> H
H --> I[HTML-Ausgabe]
end
subgraph "Template-Quellen"
J[Dateisystem] --> D
K[Datenbank] --> D
L[String] --> D
end
subgraph "Plugin-Typen"
M[Funktionen] --> D
N[Modifier] --> D
O[Block-Funktionen] --> D
P[Compiler-Funktionen] --> D
end

// Globales Template-Objekt
global $xoopsTpl;
// Oder neue Instanz
$tpl = new XoopsTpl();
// In Modulen verfügbar
$GLOBALS['xoopsTpl']->assign('myvar', $value);
MethodeParameterBeschreibung
assignstring $name, mixed $valueVariable zu Template zuweisen
assignByRefstring $name, mixed &$valueNach Referenz zuweisen
appendstring $name, mixed $value, bool $merge = falseZu Array-Variable anhängen
displaystring $templateTemplate rendern und ausgeben
fetchstring $templateTemplate rendern und zurückgeben
clearAssignstring $nameZugewiesene Variable löschen
clearAllAssign-Alle Variablen löschen
getTemplateVarsstring $name = nullZugewiesene Variablen abrufen
templateExistsstring $templatePrüfen ob Template existiert
isCachedstring $templatePrüfen ob Template gecacht ist
clearCachestring $template = nullTemplate-Cache löschen
// Einfache Zuweisung
$xoopsTpl->assign('title', 'My Page Title');
$xoopsTpl->assign('count', 42);
$xoopsTpl->assign('is_admin', true);
// Array-Zuweisung
$xoopsTpl->assign('items', [
['id' => 1, 'name' => 'Item 1'],
['id' => 2, 'name' => 'Item 2'],
]);
// Objekt-Zuweisung
$xoopsTpl->assign('user', $xoopsUser);
// Mehrere Zuweisungen
$xoopsTpl->assign([
'title' => 'My Title',
'content' => 'My Content',
'author' => 'John Doe'
]);
// Zu Array anhängen
$xoopsTpl->append('items', ['id' => 3, 'name' => 'Item 3']);
// Aus Datenbank (kompiliert)
$xoopsTpl->display('db:mymodule_index.tpl');
// Aus Dateisystem
$xoopsTpl->display('file:' . XOOPS_ROOT_PATH . '/modules/mymodule/templates/custom.tpl');
// Abrufen ohne Ausgabe
$html = $xoopsTpl->fetch('db:mymodule_item.tpl');
// Aus String
$template = '<h1>{$title}</h1><p>{$content}</p>';
$html = $xoopsTpl->fetch('string:' . $template);

{* Einfache Variable *}
<{$title}>
{* Array-Zugriff *}
<{$item.name}>
<{$item['name']}>
{* Objekt-Eigenschaft *}
<{$user->name}>
<{$user->getVar('uname')}>
{* Konfigurationsvariable *}
<{$xoops_sitename}>
{* Konstante *}
<{$smarty.const._MD_MYMODULE_TITLE}>
{* Server-Variablen *}
<{$smarty.server.REQUEST_URI}>
<{$smarty.get.id}>
<{$smarty.post.name}>
{* String-Modifier *}
<{$title|upper}>
<{$title|lower}>
<{$title|capitalize}>
<{$title|truncate:50:"..."}>
<{$content|strip_tags}>
<{$content|nl2br}>
<{$text|escape:'html'}>
<{$text|escape:'url'}>
{* Datum-Formatierung *}
<{$timestamp|date_format:"%Y-%m-%d"}>
<{$timestamp|date_format:"%B %e, %Y"}>
{* Nummern-Formatierung *}
<{$price|number_format:2:".":","}>
{* Standardwert *}
<{$optional|default:"N/A"}>
{* Verkettete Modifier *}
<{$title|strip_tags|truncate:50|escape}>
{* Array zählen *}
<{$items|@count}>
{* If/else *}
<{if $is_admin}>
<p>Admin-Inhalt</p>
<{elseif $is_moderator}>
<p>Moderator-Inhalt</p>
<{else}>
<p>Benutzer-Inhalt</p>
<{/if}>
{* Foreach-Schleife *}
<{foreach from=$items item=item key=key}>
<li><{$key}>: <{$item.name}></li>
<{/foreach}>
{* Foreach mit Eigenschaften *}
<{foreach from=$items item=item name=itemLoop}>
<{if $smarty.foreach.itemLoop.first}>
<ul>
<{/if}>
<li class="<{if $smarty.foreach.itemLoop.iteration is odd}>odd<{else}>even<{/if}>">
<{$smarty.foreach.itemLoop.iteration}>. <{$item.name}>
</li>
<{if $smarty.foreach.itemLoop.last}>
</ul>
<p>Insgesamt: <{$smarty.foreach.itemLoop.total}></p>
<{/if}>
<{/foreach}>
{* For-Schleife *}
<{for $i=1 to 10}>
<{$i}>
<{/for}>
{* While-Schleife *}
<{while $count < 10}>
<{$count}>
<{$count = $count + 1}>
<{/while}>
{* Anderes Template einbinden *}
<{include file="db:mymodule_header.tpl"}>
{* Einbindung mit Variablen *}
<{include file="db:mymodule_item.tpl" item=$currentItem showAuthor=true}>
{* Aus Theme einbinden *}
<{include file="$theme_template_set/header.tpl"}>
{* Dies ist ein Smarty-Kommentar - wird nicht in die Ausgabe gerendert *}
{*
Mehrzeiliger Kommentar
zur Erklärung des Templates
*}

{* Block nach ID rendern *}
<{xoBlock id=5}>
{* Block nach Name rendern *}
<{xoBlock name="mymodule_recent"}>
{* Alle Blöcke in Position rendern *}
<{foreach item=block from=$xoBlocks.canvas_left}>
<div class="block">
<h3><{$block.title}></h3>
<{$block.content}>
</div>
<{/foreach}>
{* Modul-Bild *}
<img src="<{$xoops_url}>/modules/<{$xoops_dirname}>/assets/images/logo.png">
{* Theme-Bild *}
<img src="<{$xoops_imageurl}>icon.png">
{* Upload-Verzeichnis *}
<img src="<{$xoops_upload_url}>/<{$item.image}>">
{* Modul-URL *}
<a href="<{$xoops_url}>/modules/<{$xoops_dirname}>/item.php?id=<{$item.id}>">
<{$item.title}>
</a>
{* Mit SEO-freundlicher URL (falls aktiviert) *}
<a href="<{$item.url}>"><{$item.title}></a>

sequenceDiagram
participant Request
participant XoopsTpl
participant Smarty
participant Cache
participant FileSystem
Request->>XoopsTpl: display('db:template.tpl')
XoopsTpl->>Smarty: Template verarbeiten
Smarty->>Cache: Kompilierter Cache prüfen
alt Cache-Treffer
Cache-->>Smarty: Kompiliertes PHP zurückgeben
else Cache-Fehlschlag
Smarty->>FileSystem: Template-Quelle laden
FileSystem-->>Smarty: Template-Inhalt
Smarty->>Smarty: In PHP kompilieren
Smarty->>Cache: Kompiliertes PHP speichern
end
Smarty->>Smarty: Kompiliertes PHP ausführen
Smarty-->>XoopsTpl: HTML-Ausgabe
XoopsTpl-->>Request: Gerendertes HTML

plugins/function.myfunction.php
function smarty_function_myfunction($params, $smarty)
{
$name = $params['name'] ?? 'World';
return "Hello, {$name}!";
}
// Verwendung im Template:
// <{myfunction name="John"}>
plugins/modifier.timeago.php
function smarty_modifier_timeago($timestamp)
{
$diff = time() - $timestamp;
if ($diff < 60) {
return 'just now';
} elseif ($diff < 3600) {
$mins = floor($diff / 60);
return "{$mins} minute(s) ago";
} elseif ($diff < 86400) {
$hours = floor($diff / 3600);
return "{$hours} hour(s) ago";
} else {
$days = floor($diff / 86400);
return "{$days} day(s) ago";
}
}
// Verwendung im Template:
// <{$item.created|timeago}>
plugins/block.cache.php
function smarty_block_cache($params, $content, $smarty, &$repeat)
{
if ($repeat) {
// Öffnungs-Tag
return '';
} else {
// Schließungs-Tag - Inhalt verarbeiten
$ttl = $params['ttl'] ?? 3600;
$key = md5($content);
// Cache prüfen...
return $content;
}
}
// Verwendung im Template:
// <{cache ttl=3600}>
// Teuer zu erzeugender Inhalt hier
// <{/cache}>

graph LR
subgraph "Optimierungs-Strategien"
A[Caching aktivieren] --> E[Schnellere Antwort]
B[Variablen minimieren] --> E
C[Komplexe Logik vermeiden] --> E
D[In PHP vorberechnen] --> E
end
  1. Template-Caching aktivieren - In der Produktion
  2. Nur benötigte Variablen zuweisen - Keine ganzen Objekte durchreichen
  3. Modifier sparsam einsetzen - In PHP formatieren wenn möglich
  4. Verschachtelte Schleifen vermeiden - Daten in PHP umstrukturieren
  5. Teure Blöcke cachen - Block-Caching für komplexe Abfragen nutzen

  • Smarty-Grundlagen
  • Theme-Entwicklung
  • Smarty 4-Migration

#xoops #api #smarty #templates #referenz