What are some best practices for implementing and managing templates in PHP to ensure code readability and maintainability?

Issue: Implementing and managing templates in PHP can become messy and hard to maintain if not done properly. To ensure code readability and maintainability, it is important to follow best practices such as separating logic from presentation, using a template engine, and organizing templates in a logical structure. Code snippet:

// Example of using a template engine (Twig) to separate logic from presentation
require_once 'vendor/autoload.php';

$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);

$template = $twig->load('index.html');

echo $template->render([
    'title' => 'Homepage',
    'content' => 'Welcome to our website!'
]);