aatis/template-renderer

There is no license information available for the latest version (1.2.1) of this package.

Template renderer of Aatis

1.2.1 2024-10-28 11:51 UTC

This package is auto-updated.

Last update: 2024-10-28 11:52:04 UTC


README

About

Customizable and easy to use template renderer based on file extension name.

Advertisement

This package is a part of Aatis and can't be used without the following packages :

Installation

composer require aatis/template-renderer

Usage

Requirements

Add the TemplateRenderer service into the Container.

# In config/services.yaml file :

include_services:
  - 'Aatis\TemplateRenderer\Service\TemplateRenderer'

Basic usage

Call render() method with template path and data array.

$templateRenderer = new TemplateRenderer();
$templateRenderer->render('path/to/template', [
    'var_name' => 'value'
]);

Custom Template Renderer

By default, this template renderer supports .html, .tpl.php and .html.twig files.

You can add your own template renderer by creating:

  • an enum with the extension you want to target with your custom renderers.
  • a class that extends AbstractTemplateRenderer.
enum ExtraTemplateFileExtension: string
{
    case EXTRA = '.extra';
}

This Custom Template Renderer must contains:

  • EXTENSION constant calling the case of the enum corresponding to the target extension.
  • render() method that will be called by the base template renderer.
class ExtraRenderer extends AbstractTemplateRenderer
{
    public const EXTENSION = ExtraTemplateFileExtension::EXTRA;

    public function render(string $template, array $vars = []): string
    {
        // Transform the special extension type template into a string
    }
}

If needed, you can use the getTemplateContent() method of the AbstractTemplateRenderer to extract the vars and get the content of the template which will be rendered.

class ExtraRenderer extends AbstractTemplateRenderer
{
    public const EXTENSION = ExtraTemplateFileExtension::EXTRA;

    public function render(string $template, array $vars = []): string
    {
        // do some stuff
        $content = $this->getTemplateContent($template, $vars);
        // do some stuff and return the content
    }
}

Finally, do not forget to add it into the TemplateRenderer configuration:

# In config/services.yaml file :

services:
  Aatis\TemplateRenderer\Service\TemplateRenderer:
    arguments:
      extraRenderers:
        - 'Namespace\Of\Your\Custom\Template\Renderer'