Skip to content

Commit

Permalink
Merge branch '3.x' into 4.x
Browse files Browse the repository at this point in the history
* 3.x:
  Add more tests
  Fix blocks not available under some circumstancies
  Remove double ; in generated code
  • Loading branch information
fabpot committed May 11, 2024
2 parents 3046c8c + 6da7a95 commit 7d54d0a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 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
11 changes: 3 additions & 8 deletions src/Node/CaptureNode.php
Expand Up @@ -22,7 +22,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 @@ -32,21 +32,16 @@ public function compile(Compiler $compiler): void
} else {
$compiler->raw("('' === \$tmp = implode('', iterator_to_array(");
}
if ($this->getAttribute('with_blocks')) {
$compiler->raw("(function () use (&\$context, \$macros, \$blocks) {\n");
} else {
$compiler->raw("(function () use (&\$context, \$macros) {\n");
}
$compiler
->raw("(function () use (&\$context, \$macros, \$blocks) {\n")
->indent()
->subcompile($this->getNode('body'))
->write("return; yield '';\n")
->outdent()
->write('})(), false))')
;
if (!$this->getAttribute('raw')) {
$compiler->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())");
$compiler->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset());");
}
$compiler->raw(';');
}
}
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 7d54d0a

Please sign in to comment.