diff --git a/phpBB/includes/bbcode.php b/phpBB/includes/bbcode.php
index 3460db4882..d5e01a7e1e 100644
--- a/phpBB/includes/bbcode.php
+++ b/phpBB/includes/bbcode.php
@@ -129,13 +129,30 @@ class bbcode
 	*/
 	function bbcode_cache_init()
 	{
-		global $phpbb_root_path, $phpEx, $config, $user, $phpbb_extension_manager, $phpbb_path_helper;
+		global $phpbb_root_path, $phpEx, $config, $user, $phpbb_extension_manager, $phpbb_path_helper, $phpbb_container;
 
 		if (empty($this->template_filename))
 		{
 			$this->template_bitfield = new bitfield($user->style['bbcode_bitfield']);
 
-			$template = new phpbb\template\twig\twig($phpbb_path_helper, $config, $user, new phpbb\template\context(), $phpbb_extension_manager);
+			$template = new \phpbb\template\twig\twig(
+				$phpbb_container->get('path_helper'),
+				$phpbb_container->get('config'),
+				$phpbb_container->get('user'),
+				new \phpbb\template\context(),
+				new \phpbb\template\twig\environment(
+					$phpbb_container->get('config'),
+					$phpbb_container->get('path_helper'),
+					$phpbb_container,
+					$phpbb_container->getParameter('core.root_path') . 'cache/',
+					$phpbb_container->get('ext.manager'),
+					new \phpbb\template\twig\loader()
+				),
+				$phpbb_container->getParameter('core.root_path') . 'cache/',
+				$phpbb_container->get('template.twig.extensions.collection'),
+				$phpbb_extension_manager
+			);
+
 			$template->set_style();
 			$template->set_filenames(array('bbcode.html' => 'bbcode.html'));
 			$this->template_filename = $template->get_source_file_for_handle('bbcode.html');
diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php
index 3657a89aa0..4b4ee10259 100644
--- a/phpBB/includes/functions_messenger.php
+++ b/phpBB/includes/functions_messenger.php
@@ -637,14 +637,30 @@ class messenger
 	*/
 	protected function setup_template()
 	{
-		global $config, $phpbb_path_helper, $user, $phpbb_extension_manager;
+		global $config, $phpbb_path_helper, $user, $phpbb_extension_manager, $phpbb_container;
 
 		if ($this->template instanceof \phpbb\template\template)
 		{
 			return;
 		}
 
-		$this->template = new \phpbb\template\twig\twig($phpbb_path_helper, $config, $user, new \phpbb\template\context(), $phpbb_extension_manager);
+		$this->template = new \phpbb\template\twig\twig(
+			$phpbb_container->get('path_helper'),
+			$phpbb_container->get('config'),
+			$phpbb_container->get('user'),
+			new \phpbb\template\context(),
+			new \phpbb\template\twig\environment(
+				$phpbb_container->get('config'),
+				$phpbb_container->get('path_helper'),
+				$phpbb_container,
+				$phpbb_container->getParameter('core.root_path') . 'cache/',
+				$phpbb_container->get('ext.manager'),
+				new \phpbb\template\twig\loader()
+			),
+			$phpbb_container->getParameter('core.root_path') . 'cache/',
+			$phpbb_container->get('template.twig.extensions.collection'),
+			$phpbb_extension_manager
+		);
 	}
 
 	/**
diff --git a/phpBB/install/index.php b/phpBB/install/index.php
index 395aff6c7d..3d5e546ffb 100644
--- a/phpBB/install/index.php
+++ b/phpBB/install/index.php
@@ -137,6 +137,7 @@ $phpbb_container_builder->set_custom_parameters(array(
 
 $phpbb_container = $phpbb_container_builder->get_container();
 $phpbb_container->register('dbal.conn.driver')->setSynthetic(true);
+$phpbb_container->register('template.twig.environment')->setSynthetic(true);
 $phpbb_container->compile();
 
 $phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));
@@ -268,7 +269,28 @@ $config = new \phpbb\config\config(array(
 $symfony_request = $phpbb_container->get('symfony_request');
 $phpbb_filesystem = $phpbb_container->get('filesystem');
 $phpbb_path_helper = $phpbb_container->get('path_helper');
-$template = new \phpbb\template\twig\twig($phpbb_path_helper, $config, $user, new \phpbb\template\context());
+$cache_path = $phpbb_root_path . 'cache/';
+
+$twig_environment = new \phpbb\template\twig\environment(
+	$config,
+	$phpbb_path_helper,
+	$phpbb_container,
+	$cache_path,
+	null,
+	$phpbb_container->get('template.twig.loader')
+);
+
+$phpbb_container->set('template.twig.environment', $twig_environment);
+$template = new \phpbb\template\twig\twig(
+	$phpbb_path_helper,
+	$config,
+	$user,
+	new \phpbb\template\context(),
+	$twig_environment,
+	$cache_path,
+	array($phpbb_container->get('template.twig.extensions.phpbb'))
+);
+
 $paths = array($phpbb_root_path . 'install/update/new/adm/style', $phpbb_admin_path . 'style');
 $paths = array_filter($paths, 'is_dir');
 $template->set_custom_style(array(
diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php
index f6d9b73178..0ba7a265e4 100644
--- a/phpBB/phpbb/template/twig/environment.php
+++ b/phpBB/phpbb/template/twig/environment.php
@@ -13,9 +13,6 @@
 
 namespace phpbb\template\twig;
 
-use Twig_Lexer;
-use Twig_LexerInterface;
-
 class environment extends \Twig_Environment
 {
 	/** @var \phpbb\config\config */
@@ -79,6 +76,7 @@ class environment extends \Twig_Environment
 		if (null === $this->lexer)
 		{
 			$this->lexer = $this->container->get('template.twig.lexer');
+			$this->lexer->set_environment($this);
 		}
 
 		return $this->lexer;
diff --git a/phpBB/phpbb/template/twig/lexer.php b/phpBB/phpbb/template/twig/lexer.php
index c5dc7273ba..a7848738bb 100644
--- a/phpBB/phpbb/template/twig/lexer.php
+++ b/phpBB/phpbb/template/twig/lexer.php
@@ -15,6 +15,11 @@ namespace phpbb\template\twig;
 
 class lexer extends \Twig_Lexer
 {
+	public function set_environment(\Twig_Environment $env)
+	{
+		$this->env = $env;
+	}
+
 	public function tokenize($code, $filename = null)
 	{
 		// Our phpBB tags