What are the best practices for separating HTML templates from PHP code in web development projects?

Separating HTML templates from PHP code is essential for maintaining clean and organized code in web development projects. One common approach is to use a templating engine like Twig or Blade, which allows you to write HTML templates separately from PHP logic. This separation makes it easier to manage and update the front-end design without having to modify the PHP code.

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

// Initialize Twig with the templates directory
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader);

// Render a template with variables
echo $twig->render('index.html', ['name' => 'John Doe']);
?>