In what situations would it be beneficial to use a template engine instead of include for including HTML code in PHP files?

Using a template engine can be beneficial when you have complex HTML code that needs to be included in multiple PHP files. Template engines make it easier to manage and update the HTML code by separating it from the PHP logic. This can improve code readability, maintenance, and reusability.

// Example of using a template engine (Twig) to include HTML code in PHP files

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

// Create a Twig environment
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);

// Render and include the HTML template
echo $twig->render('template.html', ['variable' => $value]);