Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- What is a Template Engine? 3:45
- Installing Twig 4:43
- Twig View 5 questions
- Template Language 4:28
- Escaping and Comments 3:53
- Twig Syntax 5 questions
- The Structure of Control 6:13
- Working with Date 3:17
- Filters and Functions 7 questions
- Combining Templates 7:09
- Adding a Second Page 3:23
- Sharing 6 questions
- Custom Functions with Macros 4:22
- Importing and Using Macros 3:28
- Multiple Macros 2:51
- Custom Functions 5 questions
- Extending Twig and Beyond 1:51
- Your next step 1 question

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Twig is most commonly installed using Composer. It can be installed as its own package in a standalone project, or many frameworks allow you to choose Twig as your preferred templating engine. Sometimes frameworks may have specific packages to better communicate with Twig.
Twig Documentation
Installing Twig as a Standalone
The recommended way to install Twig is via Composer:
composer require "twig/twig:^2.0"
Basic API Usage
This section gives you a brief introduction to the PHP API for Twig.
<?php
require_once '/path/to/vendor/autoload.php';
$loader = new Twig_Loader_Array([
'index' => 'Hello {{ name }}!',
]);
$twig = new Twig_Environment($loader);
echo $twig->render('index', ['name' => 'Fabien'])
Installing Twig with Slim
The slim/twig-view component allows you to render Twig templates instead of PHP templates in your application.
Step 1: Install slim/twig-view. The easiest way is through composer.
composer require slim/twig-view
Step 2: Next, you need to change the registered view component on the Slim app’s container like this:
// Get container
$container = $app->getContainer();
// Register component on container (your view or renderer)
$container['view'] = function ($c) {
$settings = $c->get('settings')['renderer'];
$view = new \Slim\Views\Twig($settings['template_path']);
return $view;
};
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up