-
Notifications
You must be signed in to change notification settings - Fork 4
Template
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.
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!"
When using the Application
class, the RendererIntegration
can be used to create the RendererInterface
without defining it manually:
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Configuration;
use Rougin\Slytherin\Template\RendererIntegration;
$config = new Configuration;
// Set the location of the template views ----
$config->set('app.views', __DIR__ . '/views');
// -------------------------------------------
// Add the integration the run the app ---
$items = array(new RendererIntegration);
$app = new Application;
echo $app->integrate($items)->run();
// ---------------------------------------
Note
The respective instance that will be created for the said integration is only limited for the third-party packages that are currently supported by Slytherin. The built-in implementation will be used if no third-party packages are installed for the said component.
Using the built-in implementation of the Template
component through RendererIntegration
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());
}