mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-25 04:46:26 +00:00
Fix 'ignore missing' when used on an 'embed' tag
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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]));
|
||||
|
||||
Reference in New Issue
Block a user