Tweak code

This commit is contained in:
Fabien Potencier
2024-01-25 07:42:57 +01:00
parent 14d3803647
commit 0d3a283907
4 changed files with 25 additions and 20 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ class BlockNode extends Node
if (!$this->getNode('body') instanceof NodeOutputInterface && $compiler->getEnvironment()->useYield()) {
// needed when body doesn't yield anything
$compiler->write("yield;\n");
$compiler->write("yield '';\n");
}
$compiler
+1 -1
View File
@@ -344,7 +344,7 @@ final class ModuleNode extends Node
}
} elseif ($compiler->getEnvironment()->useYield() && !$this->hasNodeOutputNodes($this->getNode('body'))) {
// ensure at least one yield call even for templates with no output
$compiler->write("yield;\n");
$compiler->write("yield '';\n");
}
$compiler
+12 -12
View File
@@ -33,17 +33,6 @@ class BlockTest extends NodeTestCase
{
$tests = [];
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
// line 1
public function block_foo(\$context, array \$blocks = [])
{
\$macros = \$this->macros;
yield "foo";
}
EOF
, new Environment(new ArrayLoader(), ['use_yield' => true])
];
if (!$this->getEnvironment()->useYield()) {
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
// line 1
@@ -56,12 +45,23 @@ EOF
, new Environment(new ArrayLoader())
];
} else {
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
// line 1
public function block_foo(\$context, array \$blocks = [])
{
\$macros = \$this->macros;
yield "foo";
}
EOF
, new Environment(new ArrayLoader())
];
$tests[] = [new BlockNode('foo', new Node(), 1), <<<EOF
// line 1
public function block_foo(\$context, array \$blocks = [])
{
\$macros = \$this->macros;
yield;
yield '';
}
EOF
, new Environment(new ArrayLoader())
+11 -6
View File
@@ -58,14 +58,19 @@ class TemplateWrapperTest extends TestCase
{
$twig = new Environment(new ArrayLoader([
'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}',
], ['use_yield' => false]));
$twig->addGlobal('bar', 'BAR');
]));
$wrapper = $twig->load('index');
if (!$twig->useYield()) {
$twig->addGlobal('bar', 'BAR');
ob_start();
$wrapper->displayBlock('foo', ['foo' => 'FOO']);
$wrapper = $twig->load('index');
$this->assertEquals('FOOBAR', ob_get_clean());
ob_start();
$wrapper->displayBlock('foo', ['foo' => 'FOO']);
$this->assertEquals('FOOBAR', ob_get_clean());
} else {
$this->markTestSkipped('yield not used.');
}
}
}