What are some best practices for implementing a template engine in PHP?

When implementing a template engine in PHP, it is important to separate the presentation logic from the business logic to maintain clean and organized code. Utilizing a template engine can help achieve this separation and improve code readability. Best practices include creating reusable templates, utilizing template inheritance for consistent layouts, and passing data to templates using a separate data layer.

// Example of implementing a template engine in PHP using Twig

// Include Composer autoload file
require_once 'vendor/autoload.php';

// Create a Twig loader with the path to templates directory
$loader = new \Twig\Loader\FilesystemLoader('path/to/templates');

// Create a Twig environment with the loader
$twig = new \Twig\Environment($loader);

// Render a template with data
echo $twig->render('template.twig', ['variable' => $data]);