What are the advantages of using a template engine for separating presentation logic from business logic in PHP applications?

Using a template engine helps separate presentation logic from business logic in PHP applications, making the code more maintainable and easier to understand. It allows designers to work on the presentation layer without needing to know PHP, and developers can focus on the backend logic. Additionally, template engines often provide features like template inheritance, caching, and escaping to help prevent security vulnerabilities.

// Example using the Twig template engine
require_once 'vendor/autoload.php';

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

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