The Essential Framework includes a built-in templating system by default, which uses the devcoder-xyz/php-renderer package. This simple PHP renderer allows you to create and render templates easily. You can refer to the official documentation for detailed information: devcoder-xyz/php-renderer GitHub.
However, if you prefer to use the Twig templating engine, you can easily switch by following these steps:
- Remove the devcoder-xyz/php-renderer package:
composer remove devcoder-xyz/php-renderer
- Install Twig using Composer:
composer require twig/twig
Twig will now be automatically configured and ready to use as your template engine.
By default, the template directory is set to /templates
within your project directory. You can modify this path by changing the APP_TEMPLATE_DIR
variable in your .env file or by modifying the app.template_dir
parameter in the parameters.php
file:
'app.template_dir' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'my_custom_templates',
Now, let's explore how to create and use templates with the chosen rendering engine.
Voici le sous-chapitre sur les helpers disponibles avec des exemples pour les moteurs de rendu PHP Renderer et Twig :
The Essential Framework provides a set of useful template helpers for both PHP Renderer and Twig.
The render_view
function allows you to render a view template with the provided context. It checks if a Renderer service is available and throws an exception if not. This helper is suitable for both PHP Renderer and Twig.
Usage:
// PHP Renderer Example
echo render_view('main.html.php', ['data' => $data]);
// Twig Example
echo render_view('main.html.twig', ['data' => data]);
The render
function creates an HTTP response from a view template. It is a convenient way to render views and send them as responses. This helper is suitable for both PHP Renderer and Twig.
Usage:
// PHP Renderer Example
return render('main.html.php', ['data' => $data], 200);
// Twig Example
return render('main.html.twig', ['data' => $data], 200);