Browse code

fixed "embed" support when used from "template_from_string"

Fabien Potencier authored on 12/03/2019 08:27:17
Showing 4 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 * 1.38.0 (2019-XX-XX)
2 2
 
3
+ * fixed "embed" support when used from "template_from_string"
3 4
  * added the possibility to pass a TemplateWrapper to Twig\Environment::load()
4 5
  * improved the performance of the sandbox
5 6
  * added a spaceless filter
... ...
@@ -351,7 +351,7 @@ class Environment
351 351
     {
352 352
         $key = $this->getLoader()->getCacheKey($name).$this->optionsHash;
353 353
 
354
-        return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index);
354
+        return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '___'.$index);
355 355
     }
356 356
 
357 357
     /**
... ...
@@ -443,9 +443,17 @@ class Environment
443 443
      */
444 444
     public function loadTemplate($name, $index = null)
445 445
     {
446
-        $cls = $mainCls = $this->getTemplateClass($name);
446
+        return $this->loadClass($this->getTemplateClass($name), $name, $index);
447
+    }
448
+
449
+    /**
450
+     * @internal
451
+     */
452
+    public function loadClass($cls, $name, $index = null)
453
+    {
454
+        $mainCls = $cls;
447 455
         if (null !== $index) {
448
-            $cls .= '_'.$index;
456
+            $cls .= '___'.$index;
449 457
         }
450 458
 
451 459
         if (isset($this->loadedTemplates[$cls])) {
... ...
@@ -491,7 +499,7 @@ class Environment
491 499
             }
492 500
 
493 501
             if (!class_exists($cls, false)) {
494
-                throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source);
502
+                throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.', $name, $index), -1, $source);
495 503
             }
496 504
         }
497 505
 
... ...
@@ -351,6 +351,15 @@ abstract class Template implements \Twig_TemplateInterface
351 351
                 return $template;
352 352
             }
353 353
 
354
+            if ($template === $this->getTemplateName()) {
355
+                $class = get_class($this);
356
+                if (false !== $pos = strrpos($class, '___', -1)) {
357
+                    $class = substr($class, 0, $pos);
358
+                }
359
+
360
+                return $this->env->loadClass($class, $template, $index);
361
+            }
362
+
354 363
             return $this->env->loadTemplate($template, $index);
355 364
         } catch (Error $e) {
356 365
             if (!$e->getSourceContext()) {
357 366
new file mode 100644
... ...
@@ -0,0 +1,11 @@
1
+--TEST--
2
+"template_from_string" function works in an "include"
3
+--TEMPLATE--
4
+{% set embed = '{% embed "embed.twig" %}{% endembed %}' %}
5
+{{ include(template_from_string(embed)) }}
6
+--TEMPLATE(embed.twig)--
7
+Cool
8
+--DATA--
9
+return []
10
+--EXPECT--
11
+Cool