How can PHP developers integrate external template systems like Twig into their projects?
PHP developers can integrate external template systems like Twig into their projects by first installing Twig through Composer. Once Twig is installed, developers can create Twig templates with the .twig extension and render them in their PHP files using Twig's environment and loader. This allows for cleaner and more maintainable code separation between logic and presentation.
// Include Composer's autoloader
require_once 'vendor/autoload.php';
// Create a Twig loader that points to the directory where templates are stored
$loader = new Twig\Loader\FilesystemLoader('path/to/templates');
// Create a Twig environment with the loader
$twig = new Twig\Environment($loader);
// Render a Twig template
echo $twig->render('template.twig', ['variable' => 'value']);