fixed "embed" support when used from "template_from_string"

This commit is contained in:
Fabien Potencier
2019-03-12 09:27:17 +01:00
parent e7976359a9
commit 92a63e0c77
4 changed files with 33 additions and 4 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.38.0 (2019-XX-XX)
* fixed "embed" support when used from "template_from_string"
* added the possibility to pass a TemplateWrapper to Twig\Environment::load()
* improved the performance of the sandbox
* added a spaceless filter
+12 -4
View File
@@ -351,7 +351,7 @@ class Environment
{
$key = $this->getLoader()->getCacheKey($name).$this->optionsHash;
return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index);
return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '___'.$index);
}
/**
@@ -443,9 +443,17 @@ class Environment
*/
public function loadTemplate($name, $index = null)
{
$cls = $mainCls = $this->getTemplateClass($name);
return $this->loadClass($this->getTemplateClass($name), $name, $index);
}
/**
* @internal
*/
public function loadClass($cls, $name, $index = null)
{
$mainCls = $cls;
if (null !== $index) {
$cls .= '_'.$index;
$cls .= '___'.$index;
}
if (isset($this->loadedTemplates[$cls])) {
@@ -491,7 +499,7 @@ class Environment
}
if (!class_exists($cls, false)) {
throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source);
throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.', $name, $index), -1, $source);
}
}
+9
View File
@@ -351,6 +351,15 @@ abstract class Template implements \Twig_TemplateInterface
return $template;
}
if ($template === $this->getTemplateName()) {
$class = get_class($this);
if (false !== $pos = strrpos($class, '___', -1)) {
$class = substr($class, 0, $pos);
}
return $this->env->loadClass($class, $template, $index);
}
return $this->env->loadTemplate($template, $index);
} catch (Error $e) {
if (!$e->getSourceContext()) {
@@ -0,0 +1,11 @@
--TEST--
"template_from_string" function works in an "include"
--TEMPLATE--
{% set embed = '{% embed "embed.twig" %}{% endembed %}' %}
{{ include(template_from_string(embed)) }}
--TEMPLATE(embed.twig)--
Cool
--DATA--
return []
--EXPECT--
Cool