diff --git a/extra/cache-extra/Node/CacheNode.php b/extra/cache-extra/Node/CacheNode.php index de6d964fd3c..5cb73c592c1 100644 --- a/extra/cache-extra/Node/CacheNode.php +++ b/extra/cache-extra/Node/CacheNode.php @@ -40,7 +40,7 @@ public function compile(Compiler $compiler): void ->addDebugInfo($this) ->raw('$this->env->getRuntime(\'Twig\Extra\Cache\CacheRuntime\')->getCache()->get(') ->subcompile($this->getNode('key')) - ->raw(", function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context, \$macros) {\n") + ->raw(", function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context, \$macros, \$blocks) {\n") ->indent() ; diff --git a/extra/cache-extra/Tests/Fixtures/cache_complex.test b/extra/cache-extra/Tests/Fixtures/cache_complex.test new file mode 100644 index 00000000000..5a2e28f4709 --- /dev/null +++ b/extra/cache-extra/Tests/Fixtures/cache_complex.test @@ -0,0 +1,15 @@ +--TEST-- +"cache" tag +--TEMPLATE-- +{% cache 'test_%s_%s'|format(10, 10000) ttl(36000) %} + {% set content %} + OK + {% endset %} + {% apply spaceless %} + {{ content }} + {% endapply %} +{% endcache %} +--DATA-- +return [] +--EXPECT-- +OK diff --git a/extra/cache-extra/Tests/Fixtures/cache_with_blocks.test b/extra/cache-extra/Tests/Fixtures/cache_with_blocks.test new file mode 100644 index 00000000000..79721d7fca8 --- /dev/null +++ b/extra/cache-extra/Tests/Fixtures/cache_with_blocks.test @@ -0,0 +1,15 @@ +--TEST-- +"cache" tag +--TEMPLATE-- +{% extends "layout.twig" %} +{% block bar %} + {% cache "foo" %} + {%- block content %}FOO{% endblock %} + {% endcache %} +{% endblock %} +--TEMPLATE(layout.twig)-- +{% block content %}{% endblock %} +--DATA-- +return [] +--EXPECT-- +FOO diff --git a/src/Node/CaptureNode.php b/src/Node/CaptureNode.php index 534f1d01f17..d7db218c3c1 100644 --- a/src/Node/CaptureNode.php +++ b/src/Node/CaptureNode.php @@ -24,7 +24,7 @@ class CaptureNode extends Node { public function __construct(Node $body, int $lineno, ?string $tag = null) { - parent::__construct(['body' => $body], ['raw' => false, 'with_blocks' => false], $lineno, $tag); + parent::__construct(['body' => $body], ['raw' => false], $lineno, $tag); } public function compile(Compiler $compiler): void @@ -34,13 +34,9 @@ public function compile(Compiler $compiler): void if (!$this->getAttribute('raw')) { $compiler->raw("('' === \$tmp = "); } - $compiler->raw($useYield ? "implode('', iterator_to_array(" : '\\Twig\\Extension\\CoreExtension::captureOutput('); - if ($this->getAttribute('with_blocks')) { - $compiler->raw("(function () use (&\$context, \$macros, \$blocks) {\n"); - } else { - $compiler->raw("(function () use (&\$context, \$macros) {\n"); - } $compiler + ->raw($useYield ? "implode('', iterator_to_array(" : '\\Twig\\Extension\\CoreExtension::captureOutput(') + ->raw("(function () use (&\$context, \$macros, \$blocks) {\n") ->indent() ->subcompile($this->getNode('body')) ->write("return; yield '';\n") diff --git a/src/Node/MacroNode.php b/src/Node/MacroNode.php index 761ef55d2fb..78e1b6f76dd 100644 --- a/src/Node/MacroNode.php +++ b/src/Node/MacroNode.php @@ -80,7 +80,6 @@ public function compile(Compiler $compiler): void } $node = new CaptureNode($this->getNode('body'), $this->getNode('body')->lineno, $this->getNode('body')->tag); - $node->setAttribute('with_blocks', true); $compiler ->write('') diff --git a/src/Node/SetNode.php b/src/Node/SetNode.php index 6b4c873e1ab..0900f1542a9 100644 --- a/src/Node/SetNode.php +++ b/src/Node/SetNode.php @@ -38,7 +38,6 @@ public function __construct(bool $capture, Node $names, Node $values, int $linen $capture = false; } else { $values = new CaptureNode($values, $values->getTemplateLine()); - $values->setAttribute('with_blocks', true); } }