Skip to content

Commit

Permalink
fixed "embed" support when used from "template_from_string"
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 12, 2019
1 parent e797635 commit fd55e1c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/Environment.php
Expand Up @@ -351,7 +351,7 @@ public function getTemplateClass($name, $index = null)
{
$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);
}

/**
Expand Down Expand Up @@ -443,9 +443,17 @@ public function load($name)
*/
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])) {
Expand Down Expand Up @@ -491,7 +499,7 @@ public function loadTemplate($name, $index = null)
}

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);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Template.php
Expand Up @@ -351,6 +351,15 @@ protected function loadTemplate($template, $templateName = null, $line = null, $
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()) {
Expand Down

0 comments on commit fd55e1c

Please sign in to comment.