ADR-003 - mehanizam za predloške
ADR-003: predložak (Smarty)
Section titled “ADR-003: predložak (Smarty)”Zapis odluke o arhitekturi za XOOPS usvajanje mehanizma predložaka Smarty.
Status
Section titled “Status”Prihvaćeno - Temeljna odluka od XOOPS 2.0
Razvija se - planirana migracija na Smarty 4/5 za XOOPS 4.0
Kontekst
Section titled “Kontekst”XOOPS trebao je rješenje za izradu predložaka koje bi:
- Odvojite prezentaciju od poslovne logike
- Dopustite dizajnerima tema da rade bez znanja PHP
- Podržava nasljeđivanje predložaka i includes
- Omogućite predmemoriranje za performanse
- Omogućite korisnički prilagodljiv templates
- Podržite internacionalizaciju
Dijagram odluke
Section titled “Dijagram odluke”flowchart TB subgraph "PHP Layer" A[Module Controller] B[Template Variables] end
subgraph "Smarty Engine" C[Smarty Core] D[Template Compiler] E[Cache Manager] end
subgraph "Templates" F[Module Templates] G[Theme Templates] H[Block Templates] end
subgraph "Output" I[Compiled PHP] J[Cached HTML] K[Final HTML] end
A --> B B --> C C --> D C --> E D --> F D --> G D --> H F --> I G --> I H --> I E --> J I --> K J --> KOdluka
Section titled “Odluka”Koristit ćemo Smarty kao pokretač predložaka jer:
1. Razdvajanje koncerna
Section titled “1. Razdvajanje koncerna”// PHP (Controller) - Business logic$items = $itemHandler->getPublishedItems();$xoopsTpl->assign('items', $items);
// Smarty (View) - Presentation// templates/items.tpl{* Smarty template - No PHP logic *}<{foreach item=item from=$items}> <article> <h2><{$item.title}></h2> <p><{$item.summary}></p> </article><{/foreach}>2. XOOPS Razdjelnici
Section titled “2. XOOPS Razdjelnici”XOOPS koristi <{ i }> umjesto standardnog { }:
{* Standard Smarty *}{$variable}
{* XOOPS Smarty - Avoids JavaScript conflicts *}<{$variable}>3. Hijerarhija predloška
Section titled “3. Hijerarhija predloška”graph TB A[Theme Master Template<br>theme.html] --> B[Module Template<br>module_index.tpl] A --> C[Block Templates<br>block_*.tpl] B --> D[Partial Templates<br>_header.tpl] B --> E[Partial Templates<br>_footer.tpl]
style A fill:#f9f,stroke:#333 style B fill:#9ff,stroke:#333 style C fill:#ff9,stroke:#3334. Pohranjivanje predložaka
Section titled “4. Pohranjivanje predložaka”- baza podataka: prilagođeni templates pohranjen za mogućnost vraćanja
- Datotečni sustav: Izvorni templates u direktorijima modula
- predmemorija: Prevedeno templates za performanse
Smarty Konfiguracija
Section titled “Smarty Konfiguracija”// XOOPS Smarty initialization$xoopsTpl = new XoopsTpl();
// Custom delimiters$xoopsTpl->left_delim = '<{';$xoopsTpl->right_delim = '}>';
// Caching$xoopsTpl->caching = XOOPS_TEMPLATE_CACHE;$xoopsTpl->cache_lifetime = 3600;
// Security$xoopsTpl->security_policy = new Smarty_Security($xoopsTpl);$xoopsTpl->security_policy->php_functions = [];$xoopsTpl->security_policy->php_modifiers = ['escape', 'count'];Korištene značajke predloška
Section titled “Korištene značajke predloška”Varijable
Section titled “Varijable”{* Simple variable *}<{$title}>
{* Object property *}<{$item.title}>
{* With modifier *}<{$content|truncate:200:'...'}>
{* Escaped output *}<{$userInput|escape:'html'}>Kontrolne strukture
Section titled “Kontrolne strukture”{* Conditional *}<{if $isAdmin}> <a href="admin.php">Admin</a><{elseif $isUser}> <a href="profile.php">Profile</a><{else}> <a href="login.php">Login</a><{/if}>
{* Loop *}<{foreach item=item from=$items name=itemloop}> <{$smarty.foreach.itemloop.index}>: <{$item.title}><{/foreach}>Uključuje
Section titled “Uključuje”{* Include another template *}<{include file="db:mymodule_header.tpl"}>
{* Include with variables *}<{include file="db:mymodule_item.tpl" item=$currentItem}>
{* Include from theme *}<{include file="file:$theme_path/partials/sidebar.tpl"}>Posljedice
Section titled “Posljedice”Pozitivno
Section titled “Pozitivno”- Prilagođeno dizajneru: sintaksa slična HTML
- Caching: Ugrađeno predmemoriranje predložaka
- Sigurnost: izolacija koda PHP
- Fleksibilnost: Modifikatori, funkcije, dodaci
- Prilagodba: Korisnici mogu mijenjati templates
- Zajednica: Veliki ekosustav Smarty
Negativno
Section titled “Negativno”- Krivulja učenja: sintaksa specifična za Smarty
- Opšte: potreban je korak kompilacije
- Uklanjanje pogrešaka: Pogreške u predlošku mogu biti zagonetne
- Problemi s verzijom: prijelomne promjene između verzija
Ublažavanja
Section titled “Ublažavanja”- Učenje: Sveobuhvatna dokumentacija
- Performanse: Agresivno predmemoriranje
- Debugging: Debug konzola, brisanje poruka o pogreškama
- Verzije: sloj kompatibilnosti u XOOPS
Povijest verzija
Section titled “Povijest verzija”timeline title Smarty in XOOPS 2003 : Smarty 2.x : Initial integration 2013 : Smarty 3.0 : XOOPS 2.5.5 2020 : Smarty 3.1 : XOOPS 2.5.10 2026 : Smarty 4/5 : XOOPS 4.0Migracija: Smarty 3 do 4/5
Section titled “Migracija: Smarty 3 do 4/5”Prijelomne promjene
Section titled “Prijelomne promjene”{* Smarty 3 - Deprecated *}<{php}>echo date('Y');<{/php}>
{* Smarty 4+ - Use modifiers or assign from PHP *}<{$current_year}>
{* Smarty 3 - {section} deprecated *}<{section name=i loop=$items}> <{$items[i].title}><{/section}>
{* Smarty 4+ - Use {foreach} *}<{foreach $items as $item}> <{$item.title}><{/foreach}>Sloj kompatibilnosti
Section titled “Sloj kompatibilnosti”XOOPS pruža sloj kompatibilnosti za glatke prijelaze:
// XoopsTpl extends Smarty with compatibility methodsclass XoopsTpl extends Smarty{ public function assign($tpl_var, $value = null) { // Handles both Smarty 3 and 4 syntax return parent::assign($tpl_var, $value); }}Razmotrene alternative
Section titled “Razmotrene alternative”1. Grančica
Section titled “1. Grančica”Prednosti: Moderan, Symfony ekosustav Protiv: Drugačija sintaksa, napor migracije Odluka: Moguća buduća opcija za XOOPS 3.x
2. Blade (Laravel)
Section titled “2. Blade (Laravel)”Prednosti: čista sintaksa, popularan Protiv: Specifično za Laravel Odluka: Nije prikladno za samostalnu upotrebu
3. Izvorni PHP predlošci
Section titled “3. Izvorni PHP predlošci”Prednosti: Nema krivulje učenja, brzo Protiv: Sigurnosni rizici, nema odvajanja Odluka: Odbijeno zbog mogućnosti održavanja
Povezane odluke
Section titled “Povezane odluke”- ADR-001: Modularna arhitektura
- ADR-002: Apstrakcija baze podataka
Reference
Section titled “Reference”- Smarty Dokumentacija: https://www.smarty.net/docs/en/
- Vodič za sustav predložaka XOOPS
- MVC uzorak u web aplikacijama
---#xoops #arhitektura #adr #smarty #templates #dizajn-odluka