require_once 'vendor/autoload.php'; use Twig\Environment; use Twig\Loader\FilesystemLoader; $loader = new FilesystemLoader('path/to/templates'); $twig = new Environment($loader); $template = $twig->load('template.html.twig'); echo $template->render(['name' => 'John']);
require_once 'vendor/autoload.php'; use Twig\Environment; use Twig\Loader\ArrayLoader; $loader = new ArrayLoader([ 'template' => 'Hello {{ name }}!' ]); $twig = new Environment($loader); echo $twig->render('template', ['name' => 'John']);In this example, instead of using a filesystem loader, we use an array loader to store our template as a string. We load the template and pass variables to it using the render function. Overall, Twig_Environment render is a useful function for rendering templates using Twig, and can be used in a variety of scenarios to create dynamic web pages.