Zum Inhalt springen

XOOPS Template-System

Das XOOPS Template-System basiert auf der leistungsstarken Smarty Template-Engine und bietet eine flexible und erweiterbare Möglichkeit, Präsentationslogik von Geschäftslogik zu trennen. Es verwaltet Designs, Template-Rendering, Variablenzuweisung und dynamische Inhaltsgenerierung.

graph TD
A[XoopsTpl] -->|erweitert| B[Smarty]
A -->|verwaltet| C[Designs]
A -->|verwaltet| D[Template-Variablen]
A -->|handhabt| E[Block-Rendering]
C -->|enthält| F[Templates]
C -->|enthält| G[CSS/JS]
C -->|enthält| H[Bilder]
I[Theme Manager] -->|lädt| C
I -->|wendet an| J[Aktives Design]
I -->|konfiguriert| K[Template-Pfade]
L[Block-System] -->|nutzt| A
M[Modul-Templates] -->|nutzen| A
N[Admin-Templates] -->|nutzen| A

Die hauptsächliche Template-Engine-Klasse, die Smarty erweitert.

namespace Xoops\Core;
class XoopsTpl extends Smarty
{
protected array $vars = [];
protected string $currentTheme = '';
protected array $blocks = [];
protected bool $isAdmin = false;
}
use Xoops\Core\XoopsTpl;
class XoopsTpl extends Smarty
{
private static ?XoopsTpl $instance = null;
private function __construct()
{
parent::__construct();
$this->configureDirectories();
$this->registerPlugins();
}
public static function getInstance(): XoopsTpl
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
}

Ruft die Singleton-Template-Instanz ab.

public static function getInstance(): XoopsTpl

Rückgabewert: XoopsTpl - Singleton-Instanz

Beispiel:

$xoopsTpl = XoopsTpl::getInstance();

Weist eine Variable dem Template zu.

public function assign(
string|array $tplVar,
mixed $value = null
): void

Parameter:

ParameterTypBeschreibung
$tplVarstring|arrayVariablenname oder assoziatives Array
$valuemixedVariablenwert

Beispiel:

$xoopsTpl->assign('page_title', 'Welcome');
$xoopsTpl->assign('user_name', 'John Doe');
// Mehrere Zuweisungen
$xoopsTpl->assign([
'items' => $items,
'total_count' => count($items),
'show_pagination' => true
]);

Hängt Werte an Template-Array-Variablen an.

public function appendAssign(
string $tplVar,
mixed $value
): void

Parameter:

ParameterTypBeschreibung
$tplVarstringVariablenname
$valuemixedAnzuhängender Wert

Beispiel:

$xoopsTpl->assign('breadcrumbs', ['Home']);
$xoopsTpl->appendAssign('breadcrumbs', 'Blog');
$xoopsTpl->appendAssign('breadcrumbs', 'Posts');
// breadcrumbs = ['Home', 'Blog', 'Posts']

Ruft alle zugewiesenen Template-Variablen ab.

public function getAssignedVars(): array

Rückgabewert: array - Zugewiesene Variablen

Beispiel:

$vars = $xoopsTpl->getAssignedVars();
foreach ($vars as $name => $value) {
echo "$name = " . var_export($value, true) . "\n";
}

Rendert ein Template und gibt es an den Browser aus.

public function display(
string $resource,
string|array $cache_id = null,
string $compile_id = null,
object $parent = null
): void

Parameter:

ParameterTypBeschreibung
$resourcestringTemplate-Dateipfad
$cache_idstring|arrayCache-Identifikator
$compile_idstringCompile-Identifikator
$parentobjectParent-Template-Objekt

Beispiel:

$xoopsTpl->assign('page_title', 'Home');
$xoopsTpl->display('user:index.tpl');
// Mit absolutem Pfad
$xoopsTpl->display(XOOPS_ROOT_PATH . '/templates/user/index.tpl');

Rendert ein Template und gibt es als String zurück.

public function fetch(
string $resource,
string|array $cache_id = null,
string $compile_id = null,
object $parent = null
): string

Rückgabewert: string - Gerenderter Template-Inhalt

Beispiel:

$xoopsTpl->assign('message', 'Hello World');
$html = $xoopsTpl->fetch('user:message.tpl');
echo $html;
// Für E-Mail-Templates verwenden
$emailContent = $xoopsTpl->fetch('mail:notification.tpl');
mail($to, $subject, $emailContent);

Lädt ein bestimmtes Design.

public function loadTheme(string $themeName): bool

Parameter:

ParameterTypBeschreibung
$themeNamestringDesign-Verzeichnisname

Rückgabewert: bool - True bei Erfolg

Beispiel:

if ($xoopsTpl->loadTheme('bluemoon')) {
echo "Design erfolgreich geladen";
}

Ruft den Namen des aktuell aktiven Designs ab.

public function getCurrentTheme(): string

Rückgabewert: string - Design-Name

Beispiel:

$currentTheme = $xoopsTpl->getCurrentTheme();
echo "Aktives Design: $currentTheme";

Fügt einen Ausgabe-Filter zur Verarbeitung der Template-Ausgabe hinzu.

public function setOutputFilter(string $function): void

Parameter:

ParameterTypBeschreibung
$functionstringFilter-Funktionsname

Beispiel:

// Whitespace aus Ausgabe entfernen
$xoopsTpl->setOutputFilter('trim');
// Benutzerdefinierter Filter
function my_output_filter($output) {
// HTML minimieren
$output = preg_replace('/\s+/', ' ', $output);
return trim($output);
}
$xoopsTpl->setOutputFilter('my_output_filter');

Registriert ein benutzerdefinertes Smarty-Plugin.

public function registerPlugin(
string $type,
string $name,
callable $callback
): void

Parameter:

ParameterTypBeschreibung
$typestringPlugin-Typ (modifier, block, function)
$namestringPlugin-Name
$callbackcallableCallback-Funktion

Beispiel:

// Benutzerdefinierten Modifier registrieren
$xoopsTpl->registerPlugin('modifier', 'markdown', function($text) {
return markdown_parse($text);
});
// Im Template verwenden: {$content|markdown}
// Block-Tag registrieren
$xoopsTpl->registerPlugin('block', 'permission', function($params, $content, $smarty, &$repeat) {
if ($repeat) return;
// Berechtigung prüfen
if (has_permission($params['name'])) {
return $content;
}
return '';
});
// Im Template verwenden: {permission name="admin"}...{/permission}

Standardmäßige XOOPS Theme-Verzeichnisstruktur:

bluemoon/
├── style.css # Hauptstylesheet
├── admin.css # Admin-Stylesheet
├── theme.html # Haupt-Seiten-Template
├── admin.html # Admin-Seiten-Template
├── blocks/ # Block-Templates
│ ├── block_left.tpl
│ └── block_right.tpl
├── modules/ # Modul-Templates
│ ├── publisher/
│ │ ├── index.tpl
│ │ └── item.tpl
│ └── news/
│ └── index.tpl
├── images/ # Design-Bilder
│ ├── logo.png
│ └── banner.png
├── js/ # Design-JavaScript
│ └── script.js
└── readme.txt # Design-Dokumentation
namespace Xoops\Core\Theme;
class ThemeManager
{
protected array $themes = [];
protected string $activeTheme = '';
protected string $themeDirectory = '';
public function getActiveTheme(): string {}
public function setActiveTheme(string $theme): bool {}
public function getThemeList(): array {}
public function themeExists(string $name): bool {}
}

XOOPS weist automatisch mehrere globale Template-Variablen zu:

VariableTypBeschreibung
$xoops_urlstringXOOPS-Installations-URL
$xoops_userXoopsUser|nullAktuelles Benutzerobjekt
$xoops_unamestringAktueller Benutzername
$xoops_isadminboolBenutzer ist Admin
$xoops_bannerstringBanner-HTML
$xoops_notificationstringBenachrichtigungs-Markup
$xoops_versionstringXOOPS-Version

Beim Rendering von Blöcken:

VariableTypBeschreibung
$blockarrayBlock-Informationen
$block.titlestringBlock-Titel
$block.contentstringBlock-Inhalt
$block.idintBlock-ID
$block.modulestringModul-Name

Module weisen typischerweise folgende Variablen zu:

VariableTypBeschreibung
$module_namestringModul-Anzeigename
$module_dirstringModul-Verzeichnis
$xoops_module_headerstringModul CSS/JS
ModifierBeschreibungBeispiel
capitalizeErsten Buchstaben kapitalisieren{$title|capitalize}
count_charactersZeichenanzahl{$text|count_characters}
date_formatZeitstempel formatieren{$timestamp|date_format:'%Y-%m-%d'}
escapeSonderzeichen entkommen{$html|escape:'html'}
nl2brZeilenumbrüche zu <br> konvertieren{$text|nl2br}
strip_tagsHTML-Tags entfernen{$content|strip_tags}
truncateStringlänge begrenzen{$text|truncate:100}
upperIn Großbuchstaben konvertieren{$name|upper}
lowerIn Kleinbuchstaben konvertieren{$name|lower}
{* If-Anweisung *}
{if $user->isAdmin()}
<p>Admin-Inhalt</p>
{else}
<p>Benutzer-Inhalt</p>
{/if}
{* For-Schleife *}
{foreach $items as $item}
<div class="item">{$item.title}</div>
{/foreach}
{* For-Schleife mit Zähler *}
{foreach $items as $item name=item_loop}
{$smarty.foreach.item_loop.iteration}: {$item.title}
{/foreach}
{* While-Schleife *}
{while $condition}
<!-- Inhalt -->
{/while}
{* Switch-Anweisung *}
{switch $status}
{case 'draft'}<span class="draft">Entwurf</span>{break}
{case 'published'}<span class="published">Veröffentlicht</span>{break}
{default}<span class="unknown">Unbekannt</span>
{/switch}
<?php
/**
* Modul-Artikel-Listenseite
*/
include __DIR__ . '/include/common.inc.php';
$xoopsTpl = XoopsTpl::getInstance();
// Prüfen ob Modul aktiv ist
$module = xoops_getModuleByDirname('articles');
if (!$module) {
redirect_header(XOOPS_URL, 3, 'Modul nicht gefunden');
}
// Item-Handler abrufen
$itemHandler = xoops_getModuleHandler('item', 'articles');
// Paginierungsparameter abrufen
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = $module->getConfig('items_per_page') ?: 10;
$offset = ($page - 1) * $perPage;
// Kriterien erstellen
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('status', 1));
$criteria->setSort('published', 'DESC');
$criteria->setLimit($perPage);
$criteria->setStart($offset);
// Items abrufen
$items = $itemHandler->getObjects($criteria);
$total = $itemHandler->getCount(new Criteria('status', 1));
// Paginierung berechnen
$pages = ceil($total / $perPage);
// Template-Variablen zuweisen
$xoopsTpl->assign([
'module_name' => $module->getName(),
'items' => $items,
'total_items' => $total,
'current_page' => $page,
'total_pages' => $pages,
'items_per_page' => $perPage,
'show_pagination' => $pages > 1
]);
// Breadcrumbs hinzufügen
$xoopsTpl->assign('xoops_breadcrumbs', [
['url' => XOOPS_URL, 'title' => 'Home'],
['url' => $module->getUrl(), 'title' => $module->getName()],
['title' => 'Articles']
]);
// Template anzeigen
$xoopsTpl->display($module->getPath() . '/templates/user/list.tpl');
<div id="articles-list">
<h1>{$module_name|escape}</h1>
{if $items}
<div class="articles-container">
{foreach $items as $item}
<article class="article-item">
<header>
<h2>
<a href="{$item.url|escape}">
{$item.title|escape}
</a>
</h2>
<div class="meta">
<span class="author">By {$item.author|escape}</span>
<span class="date">
{$item.published|date_format:'%B %d, %Y'}
</span>
</div>
</header>
<div class="content">
<p>{$item.summary|truncate:150}</p>
</div>
<footer>
<a href="{$item.url|escape}" class="read-more">
Read More »
</a>
</footer>
</article>
{/foreach}
</div>
{* Paginierung *}
{if $show_pagination}
<nav class="pagination">
{if $current_page > 1}
<a href="?page=1" class="first">« First</a>
<a href="?page={$current_page - 1}" class="prev">‹ Previous</a>
{/if}
{for $i=1 to $total_pages}
{if $i == $current_page}
<span class="current">{$i}</span>
{else}
<a href="?page={$i}">{$i}</a>
{/if}
{/for}
{if $current_page < $total_pages}
<a href="?page={$current_page + 1}" class="next">Next ›</a>
<a href="?page={$total_pages}" class="last">Last »</a>
{/if}
</nav>
{/if}
{else}
<p class="no-items">Keine Artikel gefunden.</p>
{/if}
</div>
<?php
/**
* Benutzerdefinierte Smarty-Block-Funktion für Berechtigungsprüfung
*/
function smarty_block_permission($params, $content, $smarty, &$repeat)
{
if ($repeat) return;
if (!isset($params['name'])) {
return 'Berechtigungsname erforderlich';
}
$permName = $params['name'];
$user = $GLOBALS['xoopsUser'];
// Prüfen ob Benutzer eine Berechtigung hat
if ($user && $user->isAdmin()) {
return $content;
}
if ($user && check_user_permission($user->uid(), $permName)) {
return $content;
}
return '';
}

Registrieren und verwenden:

$xoopsTpl->registerPlugin('block', 'permission', 'smarty_block_permission');

Template:

{permission name="edit_articles"}
<button>Artikel bearbeiten</button>
{/permission}
  1. Benutzer-Inhalt entkommen - Immer |escape für benutzergenerierte Inhalte verwenden
  2. Template-Pfade verwenden - Templates relativ zum Design referenzieren
  3. Logik von Präsentation trennen - Komplexe Logik in PHP behalten
  4. Templates cachen - Template-Caching in der Produktion aktivieren
  5. Modifier korrekt verwenden - Geeignete Filter für den Kontext anwenden
  6. Blöcke organisieren - Block-Templates in dediziertem Verzeichnis platzieren
  7. Variablen dokumentieren - Alle Template-Variablen in PHP dokumentieren
  • ../Module/Module-System - Modul-System und Hooks
  • ../Kernel/Kernel-Classes - Kernel und Konfiguration
  • ../Core/XoopsObject - Basis-Objektklasse

Siehe auch: Smarty-Dokumentation | XOOPS Template API