What are the best practices for integrating Twig into a PHP project?

When integrating Twig into a PHP project, it is best practice to set up a separate directory for your Twig templates, use namespaces to autoload Twig classes, and pass variables from your PHP code to your Twig templates using the render method. Additionally, make sure to sanitize any user input before passing it to Twig to prevent security vulnerabilities.

// Autoload Twig classes using namespaces
require_once 'vendor/autoload.php';

use Twig\Environment;
use Twig\Loader\FilesystemLoader;

// Set up Twig
$loader = new FilesystemLoader('templates');
$twig = new Environment($loader);

// Pass variables from PHP to Twig
$template = $twig->load('index.html');
echo $template->render(['title' => 'Welcome', 'content' => 'Hello, world!']);