diff --git a/composer.lock b/composer.lock
index 0b63316..75914e5 100644
--- a/composer.lock
+++ b/composer.lock
@@ -2420,30 +2420,31 @@
         },
         {
             "name": "twig/twig",
-            "version": "v1.32.0",
+            "version": "v2.3.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/twigphp/Twig.git",
-                "reference": "9935b662e24d6e634da88901ab534cc12e8c728f"
+                "reference": "437efc435619c8e67acacda37b3984344854142c"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/twigphp/Twig/zipball/9935b662e24d6e634da88901ab534cc12e8c728f",
-                "reference": "9935b662e24d6e634da88901ab534cc12e8c728f",
+                "url": "https://api.github.com/repos/twigphp/Twig/zipball/437efc435619c8e67acacda37b3984344854142c",
+                "reference": "437efc435619c8e67acacda37b3984344854142c",
                 "shasum": ""
             },
             "require": {
-                "php": ">=5.2.7"
+                "php": "^7.0",
+                "symfony/polyfill-mbstring": "~1.0"
             },
             "require-dev": {
                 "psr/container": "^1.0",
                 "symfony/debug": "~2.7",
-                "symfony/phpunit-bridge": "~3.2"
+                "symfony/phpunit-bridge": "~3.3@dev"
             },
             "type": "library",
             "extra": {
                 "branch-alias": {
-                    "dev-master": "1.32-dev"
+                    "dev-master": "2.3-dev"
                 }
             },
             "autoload": {
@@ -2478,7 +2479,7 @@
             "keywords": [
                 "templating"
             ],
-            "time": "2017-02-27T00:07:03+00:00"
+            "time": "2017-04-18T21:50:11+00:00"
         },
         {
             "name": "wikimedia/composer-merge-plugin",
diff --git a/core/composer.json b/core/composer.json
index 82ea0e6..28d4053 100644
--- a/core/composer.json
+++ b/core/composer.json
@@ -18,7 +18,7 @@
         "symfony/process": "~2.8",
         "symfony/polyfill-iconv": "~1.0",
         "symfony/yaml": "~2.8",
-        "twig/twig": "^1.23.1",
+        "twig/twig": "^1.23.1|^2",
         "doctrine/common": "^2.5",
         "doctrine/annotations": "1.2.*",
         "guzzlehttp/guzzle": "^6.2.1",
diff --git a/core/lib/Drupal/Core/Template/Loader/StringLoader.php b/core/lib/Drupal/Core/Template/Loader/StringLoader.php
index 6325b9e..d0590a2 100644
--- a/core/lib/Drupal/Core/Template/Loader/StringLoader.php
+++ b/core/lib/Drupal/Core/Template/Loader/StringLoader.php
@@ -44,6 +44,14 @@ public function getSource($name) {
   /**
    * {@inheritdoc}
    */
+  public function getSourceContext($name) {
+    $name = (string) $name;
+    return new \Twig_Source($name, $name);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function getCacheKey($name) {
     return $name;
   }
diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php
index 21755a5..5271155 100644
--- a/core/lib/Drupal/Core/Template/TwigEnvironment.php
+++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
@@ -25,6 +25,8 @@ class TwigEnvironment extends \Twig_Environment {
 
   protected $twigCachePrefix = '';
 
+  protected $templateClassPrefix = '__TwigTemplate_';
+
   /**
    * Constructs a TwigEnvironment object and stores cache and storage
    * internally.
@@ -42,7 +44,7 @@ class TwigEnvironment extends \Twig_Environment {
    * @param array $options
    *   The options for the Twig environment.
    */
-  public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) {
+  public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, array $options = []) {
     // Ensure that twig.engine is loaded, given that it is needed to render a
     // template because functions like TwigExtension::escapeFilter() are called.
     require_once $root . '/core/themes/engines/twig/twig.engine';
@@ -57,11 +59,6 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension
     ];
     // Ensure autoescaping is always on.
     $options['autoescape'] = 'html';
-
-    $policy = new TwigSandboxPolicy();
-    $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
-    $this->addExtension($sandbox);
-
     if ($options['cache'] === TRUE) {
       $current = $state->get('twig_extension_hash_prefix', ['twig_extension_hash' => '']);
       if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) {
@@ -78,8 +75,11 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension
       $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix);
     }
 
-    $this->loader = $loader;
-    parent::__construct($this->loader, $options);
+    $this->setLoader($loader);
+    parent::__construct($this->getLoader(), $options);
+    $policy = new TwigSandboxPolicy();
+    $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
+    $this->addExtension($sandbox);
   }
 
   /**
@@ -110,7 +110,7 @@ public function getTemplateClass($name, $index = NULL) {
     // node.html.twig for the output of each node and the same compiled class.
     $cache_index = $name . (NULL === $index ? '' : '_' . $index);
     if (!isset($this->templateClasses[$cache_index])) {
-      $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index);
+      $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->getLoader()->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index);
     }
     return $this->templateClasses[$cache_index];
   }
@@ -140,7 +140,7 @@ public function getTemplateClass($name, $index = NULL) {
   public function renderInline($template_string, array $context = []) {
     // Prefix all inline templates with a special comment.
     $template_string = '{# inline_template_start #}' . $template_string;
-    return Markup::create($this->loadTemplate($template_string, NULL)->render($context));
+    return Markup::create($this->createTemplate($template_string)->render($context));
   }
 
 }
diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php
index 520a8be..267e109 100644
--- a/core/lib/Drupal/Core/Template/TwigExtension.php
+++ b/core/lib/Drupal/Core/Template/TwigExtension.php
@@ -65,7 +65,7 @@ class TwigExtension extends \Twig_Extension {
    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
    *   The date formatter.
    */
-  public function __construct(RendererInterface $renderer, UrlGeneratorInterface $url_generator, ThemeManagerInterface $theme_manager, DateFormatterInterface $date_formatter) {
+  public function __construct(RendererInterface $renderer, UrlGeneratorInterface $url_generator = NULL, ThemeManagerInterface $theme_manager = NULL, DateFormatterInterface $date_formatter = NULL) {
     $this->renderer = $renderer;
     $this->urlGenerator = $url_generator;
     $this->themeManager = $theme_manager;
@@ -177,7 +177,7 @@ public function getFilters() {
       new \Twig_SimpleFilter('safe_join', [$this, 'safeJoin'], ['needs_environment' => TRUE, 'is_safe' => ['html']]),
 
       // Array filters.
-      new \Twig_SimpleFilter('without', 'twig_without'),
+      new \Twig_SimpleFilter('without', [$this, 'withoutFilter']),
 
       // CSS class and ID filters.
       new \Twig_SimpleFilter('clean_class', '\Drupal\Component\Utility\Html::getClass'),
@@ -630,4 +630,37 @@ public function createAttribute(array $attributes = []) {
     return new Attribute($attributes);
   }
 
+  /**
+   * Removes child elements from a copy of the original array.
+   *
+   * Creates a copy of the renderable array and removes child elements by key
+   * specified through filter's arguments. The copy can be printed without these
+   * elements. The original renderable array is still available and can be used
+   * to print child elements in their entirety in the twig template.
+   *
+   * @param array|object $element
+   *   The parent renderable array to exclude the child items.
+   * @param string[] $args, ...
+   *   The string keys of $element to prevent printing.
+   *
+   * @return array
+   *   The filtered renderable array.
+   */
+  public function withoutFilter($element) {
+    if ($element instanceof \ArrayAccess) {
+      $filtered_element = clone $element;
+    }
+    else {
+      $filtered_element = $element;
+    }
+    $args = func_get_args();
+    unset($args[0]);
+    foreach ($args as $arg) {
+      if (isset($filtered_element[$arg])) {
+        unset($filtered_element[$arg]);
+      }
+    }
+    return $filtered_element;
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php
index 264a511..06fc8dd 100644
--- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php
+++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php
@@ -17,13 +17,21 @@ class TwigNodeTrans extends \Twig_Node {
   /**
    * {@inheritdoc}
    */
-  public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $options = NULL, $lineno, $tag = NULL) {
-    parent::__construct([
-      'count' => $count,
-      'body' => $body,
-      'plural' => $plural,
-      'options' => $options,
-    ], [], $lineno, $tag);
+  public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node $options = NULL, $lineno, $tag = NULL) {
+    $nodes = [];
+    if (NULL !== $body) {
+      $nodes['body'] = $body;
+    }
+    if (NULL !== $count) {
+      $nodes['count'] = $count;
+    }
+    if (NULL !== $plural) {
+      $nodes['plural'] = $plural;
+    }
+    if (NULL !== $options) {
+      $nodes['options'] = $options;
+    }
+    parent::__construct($nodes, [], $lineno, $tag);
   }
 
   /**
@@ -32,29 +40,27 @@ public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_N
   public function compile(\Twig_Compiler $compiler) {
     $compiler->addDebugInfo($this);
 
-    $options = $this->getNode('options');
-
     list($singular, $tokens) = $this->compileString($this->getNode('body'));
     $plural = NULL;
 
-    if (NULL !== $this->getNode('plural')) {
+    if ($this->hasNode('plural')) {
       list($plural, $pluralTokens) = $this->compileString($this->getNode('plural'));
       $tokens = array_merge($tokens, $pluralTokens);
     }
 
     // Start writing with the function to be called.
-    $compiler->write('echo ' . (empty($plural) ? 't' : '\Drupal::translation()->formatPlural') . '(');
+    $compiler->write('echo ' . ($this->hasNode('plural') ? '\Drupal::translation()->formatPlural' : 't') . '(');
 
     // Move the count to the beginning of the parameters list.
-    if (!empty($plural)) {
-      $compiler->raw('abs(')->subcompile($this->getNode('count'))->raw('), ');
+    if ($this->hasNode('plural')) {
+      $compiler->raw('abs(')->subcompile($this->hasNode('count') ? $this->getNode('count') : NULL)->raw('), ');
     }
 
     // Write the singular text parameter.
     $compiler->subcompile($singular);
 
     // Write the plural text parameter, if necessary.
-    if (!empty($plural)) {
+    if ($this->hasNode('plural')) {
       $compiler->raw(', ')->subcompile($plural);
     }
 
@@ -67,8 +73,8 @@ public function compile(\Twig_Compiler $compiler) {
     $compiler->raw(')');
 
     // Write any options passed.
-    if (!empty($options)) {
-      $compiler->raw(', ')->subcompile($options);
+    if ($this->hasNode('options')) {
+      $compiler->raw(', ')->subcompile($this->getNode('options'));
     }
 
     // Write function closure.
@@ -103,7 +109,7 @@ protected function compileString(\Twig_Node $body) {
       $text = '';
 
       foreach ($body as $node) {
-        if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof \Twig_Node_SetTemp) {
+        if (get_class($node) === 'Twig_Node') {
           $node = $node->getNode(1);
         }
 
@@ -157,7 +163,7 @@ protected function compileString(\Twig_Node $body) {
             if (!is_null($args)) {
               $argName = $args->getAttribute('name');
             }
-            $expr = new \Twig_Node_Expression_Name($argName, $n->getLine());
+            $expr = new \Twig_Node_Expression_Name($argName, $n->getTemplateLine());
           }
           $placeholder = sprintf('%s%s', $argPrefix, $argName);
           $text .= $placeholder;
@@ -176,7 +182,7 @@ protected function compileString(\Twig_Node $body) {
       $text = $body->getAttribute('data');
     }
 
-    return [new \Twig_Node([new \Twig_Node_Expression_Constant(trim($text), $body->getLine())]), $tokens];
+    return [new \Twig_Node([new \Twig_Node_Expression_Constant(trim($text), $body->getTemplateLine())]), $tokens];
   }
 
 }
diff --git a/core/lib/Drupal/Core/Template/TwigNodeVisitor.php b/core/lib/Drupal/Core/Template/TwigNodeVisitor.php
index 1ebfa57..f63edee 100644
--- a/core/lib/Drupal/Core/Template/TwigNodeVisitor.php
+++ b/core/lib/Drupal/Core/Template/TwigNodeVisitor.php
@@ -33,7 +33,7 @@ protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) {
         return $node;
       }
       $class = get_class($node);
-      $line = $node->getLine();
+      $line = $node->getTemplateLine();
       return new $class(
         new \Twig_Node_Expression_Function('render_var', new \Twig_Node([$node->getNode('expr')]), $line),
         $line
diff --git a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php
index 96f5560..559319c 100644
--- a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php
+++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php
@@ -21,10 +21,10 @@ class TwigTransTokenParser extends \Twig_TokenParser {
   public function parse(\Twig_Token $token) {
     $lineno = $token->getLine();
     $stream = $this->parser->getStream();
-    $body = NULL;
-    $options = NULL;
-    $count = NULL;
     $plural = NULL;
+    $count = NULL;
+    $options = NULL;
+    $body = NULL;
 
     if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::STRING_TYPE)) {
       $body = $this->parser->getExpressionParser()->parseExpression();
@@ -55,14 +55,14 @@ public function parse(\Twig_Token $token) {
   /**
    * Detect a 'plural' switch or the end of a 'trans' tag.
    */
-  public function decideForFork($token) {
+  public function decideForFork(\Twig_Token $token) {
     return $token->test(['plural', 'endtrans']);
   }
 
   /**
    * Detect the end of a 'trans' tag.
    */
-  public function decideForEnd($token) {
+  public function decideForEnd(\Twig_Token $token) {
     return $token->test('endtrans');
   }
 
diff --git a/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php b/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php
index b364c59..b46bb1d 100644
--- a/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php
+++ b/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php
@@ -2,11 +2,12 @@
 
 namespace Drupal\twig_extension_test\TwigExtension;
 
+use Drupal\Core\Template\TwigExtension;
 
 /**
  * A test Twig extension that adds a custom function and a custom filter.
  */
-class TestExtension extends \Twig_Extension {
+class TestExtension extends TwigExtension {
 
   /**
    * Generates a list of all Twig functions that this extension defines.
@@ -22,7 +23,7 @@ class TestExtension extends \Twig_Extension {
    */
   public function getFunctions() {
     return [
-      'testfunc' => new \Twig_Function_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction']),
+      new \Twig_SimpleFunction('testfunc', [$this, 'testFunction']),
     ];
   }
 
@@ -40,7 +41,7 @@ public function getFunctions() {
    */
   public function getFilters() {
     return [
-      'testfilter' => new \Twig_Filter_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFilter']),
+      new \Twig_SimpleFilter('testfilter', [$this, 'testFilter']),
     ];
   }
 
diff --git a/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php b/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
index 5d6f61f..d93a73d 100644
--- a/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
@@ -132,12 +132,17 @@ public function testCacheFilename() {
     $expected = strlen($prefix) + 2 + 2 * TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH;
     $this->assertEquals($expected, strlen($key));
 
-    $original_filename = $environment->getCacheFilename('core/modules/system/templates/container.html.twig');
+    $template_path = 'core/modules/system/templates/container.html.twig';
+    $cache = $environment->getCache();
+    $class = $environment->getTemplateClass($template_path);
+    $original_filename = $cache->generateKey($template_path, $class);
     \Drupal::getContainer()->set('twig', NULL);
 
     \Drupal::service('module_installer')->install(['twig_extension_test']);
     $environment = \Drupal::service('twig');
-    $new_extension_filename = $environment->getCacheFilename('core/modules/system/templates/container.html.twig');
+    $cache = $environment->getCache();
+    $class = $environment->getTemplateClass($template_path);
+    $new_extension_filename = $cache->generateKey($template_path, $class);
     \Drupal::getContainer()->set('twig', NULL);
 
     $this->assertNotEqual($new_extension_filename, $original_filename);
diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
index b9dc671..6d32554 100644
--- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
@@ -263,10 +263,10 @@ public function testChainAddRemoveClasses() {
    * @covers ::addClass
    */
   public function testTwigAddRemoveClasses($template, $expected, $seed_attributes = []) {
-    $loader = new \Twig_Loader_String();
+    $loader = new \Twig_Loader_Array([]);
     $twig = new \Twig_Environment($loader);
     $data = ['attributes' => new Attribute($seed_attributes)];
-    $result = $twig->render($template, $data);
+    $result = $twig->createTemplate($template)->render($data);
     $this->assertEquals($expected, $result);
   }
 
@@ -279,7 +279,7 @@ public function testTwigAddRemoveClasses($template, $expected, $seed_attributes
    */
   public function providerTestAttributeClassHelpers() {
     return [
-      ["{{ attributes.class }}", ''],
+      ["{{ attributes }}", ''],
       ["{{ attributes.addClass('everest').class }}", 'everest'],
       ["{{ attributes.addClass(['k2', 'kangchenjunga']).class }}", 'k2 kangchenjunga'],
       ["{{ attributes.addClass('lhotse', 'makalu', 'cho-oyu').class }}", 'lhotse makalu cho-oyu'],
diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
index c4805bc..0218856 100644
--- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
@@ -11,6 +11,19 @@
 use Drupal\Core\Url;
 use Drupal\Tests\UnitTestCase;
 
+if (!function_exists(__NAMESPACE__ . 't')) {
+
+  function t($string, array $args = []) {
+    return strtr($string, $args);
+  }
+
+}
+if (!function_exists(__NAMESPACE__ . 'file_create_url')) {
+
+  function file_create_url() {}
+
+}
+
 /**
  * Tests the twig extension.
  *
@@ -75,7 +88,8 @@ public function setUp() {
    * @dataProvider providerTestEscaping
    */
   public function testEscaping($template, $expected) {
-    $twig = new \Twig_Environment(NULL, [
+    $loader = new \Twig_Loader_Filesystem();
+    $twig = new \Twig_Environment($loader, [
       'debug' => TRUE,
       'cache' => FALSE,
       'autoescape' => 'html',
@@ -83,7 +97,8 @@ public function testEscaping($template, $expected) {
     ]);
     $twig->addExtension($this->systemUnderTest);
 
-    $nodes = $twig->parse($twig->tokenize($template));
+    $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
+    $nodes = $twig->parse($twig->tokenize(new \Twig_Source($template, $name)));
 
     $this->assertSame($expected, $nodes->getNode('body')
       ->getNode(0)
@@ -135,10 +150,11 @@ public function testActiveTheme() {
       ->method('getActiveTheme')
       ->willReturn($active_theme);
 
-    $loader = new \Twig_Loader_String();
+    $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
+    $loader = new \Twig_Loader_Array([$name => '{{ active_theme() }}']);
     $twig = new \Twig_Environment($loader);
     $twig->addExtension($this->systemUnderTest);
-    $result = $twig->render('{{ active_theme() }}');
+    $result = $twig->render($name);
     $this->assertEquals('test_theme', $result);
   }
 
@@ -172,10 +188,11 @@ public function testActiveThemePath() {
       ->method('getActiveTheme')
       ->willReturn($active_theme);
 
-    $loader = new \Twig_Loader_String();
+    $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
+    $loader = new \Twig_Loader_Array([$name => '{{ active_theme_path() }}']);
     $twig = new \Twig_Environment($loader);
     $twig->addExtension($this->systemUnderTest);
-    $result = $twig->render('{{ active_theme_path() }}');
+    $result = $twig->render($name);
     $this->assertEquals('foo/bar', $result);
   }
 
@@ -185,7 +202,8 @@ public function testActiveThemePath() {
    * @covers ::escapeFilter
    */
   public function testSafeStringEscaping() {
-    $twig = new \Twig_Environment(NULL, [
+    $loader = new \Twig_Loader_Filesystem();
+    $twig = new \Twig_Environment($loader, [
       'debug' => TRUE,
       'cache' => FALSE,
       'autoescape' => 'html',
@@ -269,7 +287,8 @@ public function providerTestRenderVar() {
    * @covers ::bubbleArgMetadata
    */
   public function testEscapeWithGeneratedLink() {
-    $twig = new \Twig_Environment(NULL, [
+    $loader = new \Twig_Loader_Filesystem();
+    $twig = new \Twig_Environment($loader, [
         'debug' => TRUE,
         'cache' => FALSE,
         'autoescape' => 'html',
@@ -327,7 +346,8 @@ public function testRenderVarWithGeneratedLink() {
    * @covers ::createAttribute
    */
   public function testCreateAttribute() {
-    $loader = new StringLoader();
+    $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
+    $loader = new \Twig_Loader_Array([$name => "{% for iteration in iterations %}<div{{ create_attribute(iteration) }}></div>{% endfor %}"]);
     $twig = new \Twig_Environment($loader);
     $twig->addExtension($this->systemUnderTest);
 
@@ -336,12 +356,15 @@ public function testCreateAttribute() {
       ['id' => 'puppies', 'data-value' => 'foo', 'data-lang' => 'en'],
       [],
     ];
-    $result = $twig->render("{% for iteration in iterations %}<div{{ create_attribute(iteration) }}></div>{% endfor %}", ['iterations' => $iterations]);
+    $result = $twig->render($name, ['iterations' => $iterations]);
     $expected = '<div class="kittens" data-toggle="modal" data-lang="es"></div><div id="puppies" data-value="foo" data-lang="en"></div><div></div>';
     $this->assertEquals($expected, $result);
 
     // Test default creation of empty attribute object and using its method.
-    $result = $twig->render("<div{{ create_attribute().addClass('meow') }}></div>");
+    $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
+    $loader = new \Twig_Loader_Array(array($name => "<div{{ create_attribute().addClass('meow') }}></div>"));
+    $twig->setLoader($loader);
+    $result = $twig->render($name);
     $expected = '<div class="meow"></div>';
     $this->assertEquals($expected, $result);
   }
