Skip to content

Commit

Permalink
bug #4081 Fix blocks not available under some circumstancies (fabpot)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Fix blocks not available under some circumstancies

Fix #4079

Commits
-------

55ae214 Add more tests
d30b72c Fix blocks not available under some circumstancies
  • Loading branch information
fabpot committed May 11, 2024
2 parents 799355d + 55ae214 commit 6da7a95
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion extra/cache-extra/Node/CacheNode.php
Expand Up @@ -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()
;

Expand Down
15 changes: 15 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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
10 changes: 3 additions & 7 deletions src/Node/CaptureNode.php
Expand Up @@ -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
Expand All @@ -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")
Expand Down
1 change: 0 additions & 1 deletion src/Node/MacroNode.php
Expand Up @@ -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('')
Expand Down
1 change: 0 additions & 1 deletion src/Node/SetNode.php
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 6da7a95

Please sign in to comment.