Twig Template Engine
MMO-DEV WEB uses Twig 3.x - a modern, fast, and secure template engine for PHP, developed by the Symfony team.
Advantages of Twig
- Security - automatic output escaping (
autoescape), protection against XSS attacks - Performance - templates are compiled into optimized PHP code and cached
- Template inheritance - powerful
extends/blocksystem for reusing layouts - Clean syntax - concise and readable template code
- Extensibility - support for custom filters, functions, and tags
Namespaces
In MMO-DEV WEB, templates are organized by namespaces, each pointing to its own directory:
| Namespace | Directory | Description |
|---|---|---|
@site | templates/site/ | Main site templates |
@admin | templates/admin/ | Admin panel templates |
@cabinet | templates/cabinet/ | User dashboard templates |
@common | templates/common/ | Shared templates (email, components) |
Usage
Template includes
{# Including a template from a specific namespace #}
{% include '@common/components/pagination.html.twig' %}
{# Extending the base site layout #}
{% extends '@site/layout.html.twig' %}
{# Including a dashboard template #}
{% include '@cabinet/partials/sidebar.html.twig' %}
Directory structure
The asset() function
To include static resources (CSS, JS, images), use the asset() function with automatic cache busting:
asset()
{# CSS #}
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
{# Result: /css/style.css?v=1a2b3c4d #}
{# JavaScript #}
<script src="{{ asset('js/app.js') }}"></script>
{# Images #}
<img src="{{ asset('images/logo.png') }}" alt="Logo">
The version parameter (?v=...) is added automatically based on the file hash, ensuring the browser cache is updated whenever the file changes.
File extension
Templates use the .html.twig extension (double extension):
- The first part (
.html) indicates the output format - The second part (
.twig) indicates the template engine
Official documentation
The full Twig documentation is available on the official website: twig.symfony.com