1.2 Twig Template Engine for PHP
In this lesson, we’ll start by writing a simple, code-only template. But you probably don’t want to mix your code and presentation like that, so I’ll teach you how to move your templates to external files. Then we’ll look at how to include templates into other templates, inherit templates from other ones, and create content blocks so that you can reuse templates for every page in your application.
Here’s an example of how to render a Twig template.
$loader = new Twig_Loader_Filesystem('templates'); $twig = new Twig_Environment($loader); echo $twig->render('greeting.html', array('person' => new Person()));
Rendering a template requires two types of objects: a template loader and an environment. The environment object takes data you provide, maps that data to template variables, and renders the template.