How can the use of a template engine, such as Twig or Smarty, benefit PHP projects in terms of separating logic from presentation?

Using a template engine like Twig or Smarty can benefit PHP projects by separating the logic (PHP code) from the presentation (HTML/CSS). This separation makes the code more organized, easier to maintain, and allows front-end developers to work on the presentation layer without needing to understand the underlying PHP logic.

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

$loader = new Twig_Loader_Filesystem('templates'); // Specify the directory where your templates are located
$twig = new Twig_Environment($loader);

$template = $twig->load('index.html'); // Load the template file

echo $template->render(array('title' => 'Welcome', 'content' => 'Hello, World!')); // Render the template with variables