diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 248c795..dd4d409 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1031,6 +1031,23 @@ function theme_disable($theme_list) { } /** + * 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. + */ +function drupal_render_twig_inline(&$variables) { + /** @var \Drupal\Core\Template\TwigEnvironment $environment */ + $environment = \Drupal::service('twig'); + return $environment->renderInlineTemplate($variables['template'], $variables['context']); +} + +/** * @addtogroup themeable * @{ */ diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php index 06f3def..4892113 100644 --- a/core/lib/Drupal/Core/Template/TwigEnvironment.php +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php @@ -74,7 +74,7 @@ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = arr /** * Checks if the compiled template needs an update. */ - public function isFresh($cache_filename, $name) { + protected function isFresh($cache_filename, $name) { $cid = 'twig:' . $cache_filename; $obj = $this->cache_object->get($cid); $mtime = isset($obj->data) ? $obj->data : FALSE; diff --git a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php index 7c100ed..7338000 100644 --- a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php +++ b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php @@ -25,6 +25,14 @@ public function testInlineTemplate() { $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'); + + $element = array(); + $element['test'] = array( + '#type' => 'twig_inline', + '#template' => 'test-with-context {{lama}}', + '#context' => array('lama' => 'muuh'), + ); + $this->assertEqual($environment->renderInlineTemplate('test-with-context {{lama}}', array('lama' => 'muuh')), 'test-with-context muuh'); } } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 117dc49..9af0e0f 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -304,6 +304,11 @@ function system_element_info() { '#theme' => 'page', '#title' => '', ); + $types['twig_inline'] = array( + '#pre_render' => 'drupal_render_twig_inline', + '#template' => '', + '#context' => array(), + ); // By default, we don't want Ajax commands being rendered in the context of an // HTML page, so we don't provide defaults for #theme or #theme_wrappers. // However, modules can set these properties (for example, to provide an HTML