Chris@0: templateClasses = []; Chris@0: Chris@0: $options += [ Chris@0: // @todo Ensure garbage collection of expired files. Chris@0: 'cache' => TRUE, Chris@0: 'debug' => FALSE, Chris@0: 'auto_reload' => NULL, Chris@0: ]; Chris@0: // Ensure autoescaping is always on. Chris@0: $options['autoescape'] = 'html'; Chris@0: Chris@0: $policy = new TwigSandboxPolicy(); Chris@0: $sandbox = new \Twig_Extension_Sandbox($policy, TRUE); Chris@0: $this->addExtension($sandbox); Chris@0: Chris@0: if ($options['cache'] === TRUE) { Chris@0: $current = $state->get('twig_extension_hash_prefix', ['twig_extension_hash' => '']); Chris@0: if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) { Chris@0: $current = [ Chris@0: 'twig_extension_hash' => $twig_extension_hash, Chris@0: // Generate a new prefix which invalidates any existing cached files. Chris@0: 'twig_cache_prefix' => uniqid(), Chris@0: Chris@0: ]; Chris@0: $state->set('twig_extension_hash_prefix', $current); Chris@0: } Chris@0: $this->twigCachePrefix = $current['twig_cache_prefix']; Chris@0: Chris@0: $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix); Chris@0: } Chris@0: Chris@0: $this->loader = $loader; Chris@0: parent::__construct($this->loader, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache Chris@0: * Chris@0: * @return string Chris@0: * The file cache prefix, or empty string if the cache is disabled. Chris@0: */ Chris@0: public function getTwigCachePrefix() { Chris@0: return $this->twigCachePrefix; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the template class associated with the given string. Chris@0: * Chris@0: * @param string $name Chris@0: * The name for which to calculate the template class name. Chris@0: * @param int $index Chris@0: * The index if it is an embedded template. Chris@0: * Chris@0: * @return string Chris@0: * The template class name. Chris@0: */ Chris@0: public function getTemplateClass($name, $index = NULL) { Chris@0: // We override this method to add caching because it gets called multiple Chris@0: // times when the same template is used more than once. For example, a page Chris@0: // rendering 50 nodes without any node template overrides will use the same Chris@0: // node.html.twig for the output of each node and the same compiled class. Chris@0: $cache_index = $name . (NULL === $index ? '' : '_' . $index); Chris@0: if (!isset($this->templateClasses[$cache_index])) { Chris@0: $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index); Chris@0: } Chris@0: return $this->templateClasses[$cache_index]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Renders a twig string directly. Chris@0: * Chris@0: * Warning: You should use the render element 'inline_template' together with Chris@0: * the #template attribute instead of this method directly. Chris@0: * On top of that you have to ensure that the template string is not dynamic Chris@0: * but just an ordinary static php string, because there may be installations Chris@0: * using read-only PHPStorage that want to generate all possible twig Chris@0: * templates as part of a build step. So it is important that an automated Chris@0: * script can find the templates and extract them. This is only possible if Chris@0: * the template is a regular string. Chris@0: * Chris@0: * @param string $template_string Chris@0: * The template string to render with placeholders. Chris@0: * @param array $context Chris@0: * An array of parameters to pass to the template. Chris@0: * Chris@0: * @return \Drupal\Component\Render\MarkupInterface|string Chris@0: * The rendered inline template as a Markup object. Chris@0: * Chris@0: * @see \Drupal\Core\Template\Loader\StringLoader::exists() Chris@0: */ Chris@0: public function renderInline($template_string, array $context = []) { Chris@0: // Prefix all inline templates with a special comment. Chris@0: $template_string = '{# inline_template_start #}' . $template_string; Chris@0: return Markup::create($this->loadTemplate($template_string, NULL)->render($context)); Chris@0: } Chris@0: Chris@0: }