Fix 'ignore missing' when used on an 'embed' tag

This commit is contained in:
Fabien Potencier
2024-09-26 07:32:30 +02:00
parent 588a5c5676
commit 09790a7542
5 changed files with 25 additions and 6 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.15.0 (2024-XX-XX)
* Fix "ignore missing" when used on an "embed" tag
* Fix the possibility to override an aliased block (via use)
* Add template cache hot reload
* Allow Twig callable argument names to be free-form (snake-case or camelCase) independently of the PHP callable signature
+8 -2
View File
@@ -33,10 +33,10 @@ class EmbedNode extends IncludeNode
$this->setAttribute('index', $index);
}
protected function addGetTemplate(Compiler $compiler): void
protected function addGetTemplate(Compiler $compiler, string $template = ''): void
{
$compiler
->write('$this->loadTemplate(')
->raw('$this->loadTemplate(')
->string($this->getAttribute('name'))
->raw(', ')
->repr($this->getTemplateName())
@@ -46,5 +46,11 @@ class EmbedNode extends IncludeNode
->string($this->getAttribute('index'))
->raw(')')
;
if ($this->getAttribute('ignore_missing')) {
$compiler
->raw(";\n")
->write(\sprintf("\$%s->getParent(\$context);\n", $template))
;
}
}
}
+4 -3
View File
@@ -48,7 +48,7 @@ class IncludeNode extends Node implements NodeOutputInterface
->write(\sprintf('$%s = ', $template))
;
$this->addGetTemplate($compiler);
$this->addGetTemplate($compiler, $template);
$compiler
->raw(";\n")
@@ -56,6 +56,7 @@ class IncludeNode extends Node implements NodeOutputInterface
->write("} catch (LoaderError \$e) {\n")
->indent()
->write("// ignore missing template\n")
->write(\sprintf("\$$template = null;\n", $template))
->outdent()
->write("}\n")
->write(\sprintf("if ($%s) {\n", $template))
@@ -78,10 +79,10 @@ class IncludeNode extends Node implements NodeOutputInterface
}
}
protected function addGetTemplate(Compiler $compiler)
protected function addGetTemplate(Compiler $compiler/* , string $template = '' */)
{
$compiler
->write('$this->loadTemplate(')
->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
@@ -0,0 +1,10 @@
--TEST--
"embed" tag
--TEMPLATE--
{% set x = 'bad' %}
{% embed x ~ 'ger.twig' ignore missing %}{% endembed %}
HERE
--DATA--
return []
--EXPECT--
HERE
+2 -1
View File
@@ -80,9 +80,10 @@ EOF
// line 1
\$__internal_%s = null;
try {
\$__internal_%s = \$this->loadTemplate("foo.twig", null, 1);
\$__internal_%s = \$this->loadTemplate("foo.twig", null, 1);
} catch (LoaderError \$e) {
// ignore missing template
\$__internal_%s = null;
}
if (\$__internal_%s) {
yield from \$__internal_%s->unwrap()->yield(CoreExtension::toArray(["foo" => true]));