ModXEngine is a lightweight PHP template engine for rendering dynamic templates with support for layouts, components, loops, and caching. It uses a <:directive:> syntax and a modular parser-based architecture.
Supports variables, loops, components, and layouts.
Enables reusable layouts and modular components.
Uses Symfony's FilesystemAdapter for efficient template caching.
Supports multiple template directories via FromArray or TemplateLoader.
Configurable layout and component folder names.
Automatically escapes variables to prevent XSS.
PHP 7.4 or higher Composer Symfony Filesystem and Cache components
Clone or Download git clone cd modxengine
composer require symfony/filesystem symfony/cache
Set Up DirectoriesCreate:
templates/: For .modx template files templates/layouts/: For layout files templates/components/: For component files cache/: For cache storage (ensure writable)
Basic Example
<?php
require 'vendor/autoload.php';
use ModXengine\View;
use ModXengine\Environment\FromArray;
// Initialize environment
$environment = FromArray::templatePath(
['templates/'], // Template directories
__DIR__, // Root path
true // Create directories if missing
);
// Initialize View
$view = new View(
environment: $environment,
cacheDir: __DIR__ . '/cache/',
layoutDir: 'layouts',
componentDir: 'components'
);
// Render template
//The with method is used to set multiple data as array while the set method is used to set single data based on key as the first variable and the value as the second
$output = $view
->with(['Apple'=>'red', 'Banana'=>'yellow', 'Orange'=>'orange'])
->set('user', 'ilem')
->layout('main')
->render('text', 3600); // Cache for 1 hour
// Cache for 1 hour
echo $output;
Alternative: Using TemplateLoader
use ModXengine\Environment\TemplateLoader;
$environment = TemplateLoader::templatePath(
'templates/',
__DIR__,
true
);
$view = new View($environment, __DIR__ . '/cache/');
//using the engine class in place of the view class
$environment = TemplateLoader::templatePath('templates');
$cache = new TemplateCache('cache', 'template', 3600);
$engine = new ModXEngine($environment, $cache);
$engine->set('food', 'yam')
->set('title', 'testing')
->set('user', 'ile,')
->with([
'test_data' => ['ssssss', 'ssssss'],
'array_test' => ['1', 2, 'testng']
])->layout("main");
echo $engine->render('text', 10);
Template Syntax
Variables: <:variable:>
<p>Welcome, <:username:>!</p>
Loops:
<:for item, index in array:> ... <:endfor:>
<ul>
<:for fruit, index in fruits:>
<li><:index:>: <:fruit:></li>
<:endfor:>
</ul>
Layouts:
<:layout "layout_name":>
<:layout "main":>
<h1>My Page</h1>
Components:
<:component "component_name">
<:component "header" >
<!--
Data is passed into the component via the set method, data passed generally into the template is available to all the components of the template
-->
Layout Content Placeholder:
<:content:>
<!-- layouts/main.modx -->
<html>
<body>
<:content:>
</body>
</html>
comment is added using the # sign and must end with the # sign
# comment goes here #
Environment Configuration
FromArray: For multiple template directories.
$environment = FromArray::templatePath(['templates/', 'other_templates/'], __DIR__);
TemplateLoader: For a single template directory.
$environment = TemplateLoader::templatePath('templates/', __DIR__);
Add Paths: Dynamically add template directories.
$environment->addPath('new_templates/');
Create Directories: Automatically create missing directories by passing true as the third variable in your environment class.
$environment = FromArray::templatePath(['templates/'], __DIR__, true);
Templates are cached with a default TTL of 3600 seconds. Override when rendering:
$view->render('index', 7200); // Cache for 2 hours
The Cacheing system depends on symfony Cache
$view->clearCache('index'); // Clear specific template
$view->clearCache(); // Clear all templates
Template Paths: Configure via FromArray or TemplateLoader. Layout/Component Directories: Set custom names in View constructor. Cache Settings: Adjust cacheNamespace and cacheTtl.
Error Handling Wrap rendering in try-catch blocks:
try {
$output = $view->render('index');
echo $output;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Exceptions are thrown for:
Missing template/layout/component files Invalid paths Rendering errors Empty output
Submit pull requests or open issues on the repository.
MIT License
Generated on May 17, 2025