In what scenarios would it be better to use a template engine like Twig instead of embedding PHP directly in HTML?

Using a template engine like Twig is beneficial when working on projects that require separation of concerns between PHP logic and HTML presentation. It helps to keep the codebase clean, maintainable, and easier to read by separating the presentation layer from the business logic. Additionally, Twig provides features like inheritance, macros, and filters that make it easier to manage and reuse code.

<?php
require_once 'vendor/autoload.php';

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

echo $twig->render('index.html', ['name' => 'John Doe']);
?>