Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:2Length:13 minutes
  • Overview
  • Transcript

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.

Code Snippet

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.

Related Links

1.Twig Template Engine for PHP
2 lessons, 13:22

Free Lesson
1.1
Introduction
01:00

1.2
Twig Template Engine for PHP
12:22