Commit 0bf30e29 authored by alexpott's avatar alexpott

Issue #2369981 by Cottser: Not found templates are displayed literally instead...

Issue #2369981 by Cottser: Not found templates are displayed literally instead of throwing an Exception due to string loader
parent 67cb2182
......@@ -1227,7 +1227,7 @@ services:
tags:
- { name: twig.loader, priority: 0 }
twig.loader.string:
class: Twig_Loader_String
class: Drupal\Core\Template\Loader\StringLoader
tags:
- { name: twig.loader, priority: -100 }
element_info:
......
<?php
/**
* @file
* Contains \Drupal\Core\Template\Loader\StringLoader.
*/
namespace Drupal\Core\Template\Loader;
/**
* Loads string templates, also known as inline templates.
*
* This loader is intended to be used in a Twig loader chain and whitelists
* string templates that begin with the following comment:
* @code
* {# inline_template_start #}
* @endcode
*
* This class override ensures that the string loader behaves as expected in
* the loader chain. If Twig's string loader is used as is, any string (even a
* reference to a file-based Twig template) is treated as a valid template and
* is rendered instead of a \Twig_Error_Loader exception being thrown.
*
* @see \Drupal\Core\Template\TwigEnvironment::renderInline()
* @see \Drupal\Core\Render\Element\InlineTemplate
* @see twig_render_template()
*/
class StringLoader extends \Twig_Loader_String {
/**
* {@inheritdoc}
*/
public function exists($name) {
if (strpos($name, '{# inline_template_start #}') === 0) {
return TRUE;
}
else {
return FALSE;
}
}
}
......@@ -195,8 +195,12 @@ public function getTemplateClass($name, $index = NULL) {
*
* @return string
* The rendered inline template.
*
* @see \Drupal\Core\Template\Loader\StringLoader::exists()
*/
public function renderInline($template_string, array $context = array()) {
// Prefix all inline templates with a special comment.
$template_string = '{# inline_template_start #}' . $template_string;
return $this->loadTemplate($template_string, NULL)->render($context);
}
......
......@@ -65,5 +65,21 @@ public function testInlineTemplate() {
$this->assertEqual(drupal_render($element_copy), 'test-with-context muuh');
}
/**
* Tests that exceptions are thrown when a template is not found.
*/
public function testTemplateNotFoundException() {
/** @var \Drupal\Core\Template\TwigEnvironment $environment */
$environment = \Drupal::service('twig');
try {
$environment->loadTemplate('this-template-does-not-exist.html.twig')->render(array());
$this->fail('Did not throw an exception as expected.');
}
catch (\Twig_Error_Loader $e) {
$this->assertTrue(strpos($e->getMessage(), 'Template "this-template-does-not-exist.html.twig" is not defined') === 0);
}
}
}
{#
/**
* @file
* Default theme implementation for the common test render element.
*
* Available variables:
* - foo: A render array.
*
* @ingroup themeable
*/
#}
{{ foo }}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment