diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php
index c671899..2c00a1f 100644
--- a/core/lib/Drupal/Core/Template/TwigEnvironment.php
+++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
@@ -31,6 +31,13 @@ class TwigEnvironment extends \Twig_Environment {
   protected $templateClasses;
 
   /**
+   * The string loader implementation used for inline template rendering.
+   *
+   * @var \Twig_Loader_String
+   */
+  protected $stringLoader;
+
+  /**
    * Constructs a TwigEnvironment object and stores cache and storage
    * internally.
    */
@@ -55,6 +62,7 @@ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = arr
     }
 
     $this->templateClasses = array();
+    $this->stringLoader = new \Twig_Loader_String();
 
     parent::__construct($loader, $options);
   }
@@ -63,17 +71,17 @@ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = arr
    * Checks if the compiled template needs an update.
    */
   public function needsUpdate($cache_filename, $name) {
-     $cid = 'twig:' . $cache_filename;
-     $obj = $this->cache_object->get($cid);
-     $mtime = isset($obj->data) ? $obj->data : FALSE;
-     return $mtime !== FALSE && !$this->isTemplateFresh($name, $mtime);
+    $cid = 'twig:' . $cache_filename;
+    $obj = $this->cache_object->get($cid);
+    $mtime = isset($obj->data) ? $obj->data : FALSE;
+    return $mtime !== FALSE && !$this->isTemplateFresh($name, $mtime);
   }
 
   /**
    * Compile the source and write the compiled template to disk.
    */
-  public function updateCompiledTemplate($cache_filename, $name) {
-    $source = $this->loader->getSource($name);
+  public function updateCompiledTemplate($cache_filename, $name, $inline = FALSE) {
+    $source = $this->getLoader($inline)->getSource($name);
     $compiled_source = $this->compileSource($source, $name);
     $this->storage()->save($cache_filename, $compiled_source);
     // Save the last modification time
@@ -81,6 +89,13 @@ public function updateCompiledTemplate($cache_filename, $name) {
     $this->cache_object->set($cid, REQUEST_TIME);
   }
 
+  public function getLoader($inline = FALSE) {
+    if (NULL === $this->loader) {
+      throw new LogicException('You must set a loader first.');
+    }
+    return $inline ? $this->stringLoader : $this->loader;
+  }
+
   /**
    * Implements Twig_Environment::loadTemplate().
    *
@@ -89,8 +104,8 @@ public function updateCompiledTemplate($cache_filename, $name) {
    * This is a straight copy from loadTemplate() changed to use
    * drupal_php_storage().
    */
-  public function loadTemplate($name, $index = NULL) {
-    $cls = $this->getTemplateClass($name, $index);
+  public function loadTemplate($name, $index = NULL, $inline = FALSE) {
+    $cls = $this->getTemplateClass($name, $index, $inline);
 
     if (isset($this->loadedTemplates[$cls])) {
       return $this->loadedTemplates[$cls];
@@ -100,19 +115,19 @@ public function loadTemplate($name, $index = NULL) {
       $cache_filename = $this->getCacheFilename($name);
 
       if ($cache_filename === FALSE) {
-        $source = $this->loader->getSource($name);
-        $compiled_source = $this->compileSource($source, $name);
+        $compiled_source = $this->compileSource($this->getLoader($inline)->getSource($name), $name);
         eval('?' . '>' . $compiled_source);
-      } else {
+      }
+      else {
 
         // If autoreload is on, check that the template has not been
         // modified since the last compilation.
         if ($this->isAutoReload() && $this->needsUpdate($cache_filename, $name)) {
-          $this->updateCompiledTemplate($cache_filename, $name);
+          $this->updateCompiledTemplate($cache_filename, $name, $inline);
         }
 
         if (!$this->storage()->load($cache_filename)) {
-          $this->updateCompiledTemplate($cache_filename, $name);
+          $this->updateCompiledTemplate($cache_filename, $name, $inline);
           $this->storage()->load($cache_filename);
         }
       }
@@ -140,16 +155,31 @@ protected function storage() {
   /**
    * {@inheritdoc}
    */
-  public function getTemplateClass($name, $index = null) {
+  public function getTemplateClass($name, $index = NULL, $inline = FALSE) {
     // We override this method to add caching because it gets called multiple
     // times when the same template is used more than once. For example, a page
     // rendering 50 nodes without any node template overrides will use the same
     // 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] = parent::getTemplateClass($name, $index);
+      $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->getLoader($inline)->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index);
     }
     return $this->templateClasses[$cache_index];
   }
 
+  /**
+   * Renders a twig string directly.
+   *
+   * @param string $template_string
+   *   The template string to render with placeholders.
+   * @param array $context
+   *   An array of parameters to pass to the template.
+   *
+   * @return string
+   *   The rendered inline template.
+   */
+  public function renderInlineTemplate($template_string, array $context = array()) {
+    return $this->loadTemplate($template_string, NULL, TRUE)->render($context);
+  }
+
 }
diff --git a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php
new file mode 100644
index 0000000..f719bc6
--- /dev/null
+++ b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Tests\Theme\TwigEnvironmentTest.
+ */
+
+namespace Drupal\system\Tests\Theme;
+
+use Drupal\simpletest\KernelTestBase;
+
+/**
+ * Tests the twig enviroment.
+ *
+ * @see \Drupal\Core\Template\TwigEnviroment
+ * @group Twig
+ */
+class TwigEnvironmentTest extends KernelTestBase {
+
+  /**
+   * Tests inline templates.
+   */
+  public function testInlineTemplate() {
+    /** @var \Drupal\Core\Template\TwigEnvironment $environment */
+    $environment = \Drupal::service('twig');
+    $this->assertEqual($environment->renderInlineTemplate('test-no-context'), 'test-no-context');
+    $this->assertEqual($environment->renderInlineTemplate('test-with-context {{lama}}', array('lama' => 'muuh')), 'test-with-context muuh');
+  }
+
+}
+
