Skip to content
Rougin Gutib edited this page Dec 22, 2023 · 9 revisions

The Template is an optional component for Slytherin in handling HTML-like views. Although PHP as the programming language is a template engine, having an abstract for the said template engine can add additional features such as separating concerns between the data and its corresponding view template.

Example Code

Slytherin provided an implementation for its own Template component. The said component is implemented by the Rougin\Slytherin\Template\RendererInterface:

// /views/hello.php

"Hello world!"
// app/web/index.php

use Rougin\Slytherin\Template\Renderer;

// Set the location of the template views ---
$path = __DIR__ . '/views';
// ------------------------------------------

$renderer = new Renderer($path);

echo $renderer->render('hello');

The example code above should return Hello world! after executing the said code in a terminal:

$ php app/web/index.php
"Hello world!"

Third party implementations

The built-in implementation of the Template component only provides basic functionalities like passing data to the template view. For other functionalities related to templating, Slytherin supports the following third-party packages below that can be integrated seamlessly to Slytherin core once their respective packages were installed:

  • TwigRenderer - uses the Twig package by Symfony (twig/twig)

If not using the supported third-party packages above, a list of third-party packages that implements the templating functionality are also listed at the awesome-php repository. Once selected a specified package, it must be implemented in RendererInterface:

namespace Rougin\Slytherin\Template;

interface RendererInterface
{
    /**
     * Renders a template.
     *
     * @param  string               $template
     * @param  array<string, mixed> $data
     * @return string
     */
    public function render($template, array $data = array());
}