derafu/renderer

Derafu: Renderer - Unified Template Rendering Made Simple For PHP

dev-main 2025-02-19 14:57 UTC

This package is auto-updated.

Last update: 2025-02-19 14:59:24 UTC


README

CI Workflow License

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.