Are there any best practices for implementing a template system in PHP?

When implementing a template system in PHP, it is important to separate the presentation layer from the business logic to improve code maintainability and reusability. One best practice is to use a templating engine like Twig or Blade to keep the HTML markup separate from the PHP code. Additionally, creating reusable template files for common elements like headers, footers, and navigation menus can help streamline the development process.

<?php
// Include the Twig autoloader
require_once 'vendor/autoload.php';

// Specify the location of the template files
$loader = new Twig_Loader_Filesystem('templates');

// Initialize the Twig environment
$twig = new Twig_Environment($loader);

// Render a template file
echo $twig->render('index.html', ['title' => 'Home Page']);
?>