Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocks not available under some circumstancies #4081

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion extra/cache-extra/Node/CacheNode.php
Original file line number Diff line number Diff line change
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug is independent of the new CaptureNode feature, but it's now more obvious.

->indent()
;

Expand Down
15 changes: 15 additions & 0 deletions extra/cache-extra/Tests/Fixtures/cache_complex.test
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$blocks should always be passed to the anonymous function as used by many Twig features.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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