From d30b72c36474ca4bea543794f879d0b4673fb254 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 May 2024 08:44:27 +0200 Subject: [PATCH 1/2] Fix blocks not available under some circumstancies --- extra/cache-extra/Node/CacheNode.php | 2 +- src/Node/CaptureNode.php | 10 +++------- src/Node/MacroNode.php | 1 - src/Node/SetNode.php | 1 - 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/extra/cache-extra/Node/CacheNode.php b/extra/cache-extra/Node/CacheNode.php index de6d964fd..5cb73c592 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/src/Node/CaptureNode.php b/src/Node/CaptureNode.php index 534f1d01f..d7db218c3 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 761ef55d2..78e1b6f76 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 6b4c873e1..0900f1542 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); } } From 55ae21493b5770634c95a23434f7c97dd6065d6e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 May 2024 08:59:04 +0200 Subject: [PATCH 2/2] Add more tests --- .../cache-extra/Tests/Fixtures/cache_complex.test | 15 +++++++++++++++ .../Tests/Fixtures/cache_with_blocks.test | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 extra/cache-extra/Tests/Fixtures/cache_complex.test create mode 100644 extra/cache-extra/Tests/Fixtures/cache_with_blocks.test 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 000000000..5a2e28f47 --- /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 000000000..79721d7fc --- /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