derafu / renderer
Derafu: Renderer - Unified Template Rendering Made Simple For PHP
dev-main
2025-02-19 14:57 UTC
Requires
- php: ^8.3
- derafu/twig: dev-main
Requires (Dev)
- ext-xdebug: *
- derafu/markdown: ^1.0.2
- friendsofphp/php-cs-fixer: ^3.63
- mpdf/mpdf: ^8.2
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.4
Suggests
- derafu/markdown: Required by MarkdownHtmlEngine.
- mpdf/mpdf: Required by HtmlPdfEngine.
This package is auto-updated.
Last update: 2025-02-19 14:59:24 UTC
README
A modern, flexible PHP template rendering library that provides a unified interface for multiple template engines and output formats.
Features
- ð Unified Interface: One consistent API for all template engines.
- ð Multiple Engine Support: Works with Twig, PHP, Markdown and more.
- ð Multiple Output Formats: Generate HTML, PDF from any template.
- ð Extensible Architecture: Easy to add new engines and formats.
- ðĻ Powerful Formatting System: Format data consistently across all templates.
- ðĄïļ Secure by Design: Safe template rendering and file handling.
- ðŠķ Lightweight Core: Only load what you need.
- ⥠Framework Agnostic: Use with any PHP framework.
Why Derafu\Renderer?
Traditional template systems often lock you into a single engine or require different handling for each format. Derafu\Renderer solves this by providing:
- A single, clean API for all template engines.
- Seamless switching between output formats.
- Consistent data formatting across all templates.
- Framework-agnostic design.
- Easy integration with existing systems.
Installation
Install via Composer:
composer require derafu/renderer
Basic Usage
use Derafu\Renderer\Factory\RendererFactory; // Create renderer with engines Twig and PDF. $renderer = RendererFactory::create([ 'engines' => ['twig', 'pdf'], 'paths' => ['/path/to/templates'], ]); // Render templates in different engines. $html = $renderer->render('template.html.twig', ['name' => 'John']); $pdf = $renderer->render('template.html.twig', ['name' => 'John'], ['engine' => 'pdf']);
Template Engines
Twig Templates
// template.html.twig <h1>Hello {{ name }}!</h1> <p>Today is {{ date|format_as('date.long') }}</p>
PHP Templates
// template.php <h1>Hello <?= $name ?>!</h1> <p>Today is <?= $format_as($date, 'date.long') ?></p>
Markdown Templates
# Hello {{ name }}!
Today is {{ date }}
Advanced Usage
Custom Engine Configuration
$renderer = RendererFactory::create([ 'engines' => ['twig', 'markdown', 'pdf'], 'paths' => ['/path/to/templates'], 'formatters' => [ 'date' => function (string $date): string { $timestamp = strtotime($date); return date('d/m/Y', $timestamp); }, ], ]);
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This library is licensed under the MIT License. See the LICENSE
file for more details.