fixed "include" with "ignore missing" when an error loading occurs in the included template

This commit is contained in:
Fabien Potencier
2019-04-05 19:11:41 +02:00
parent 22abafb081
commit 49725a1e8e
6 changed files with 66 additions and 26 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.39.0 (2019-XX-XX)
* fixed "include" with "ignore missing" when an error loading occurs in the included template
* added support for a new whitespace trimming option ({%~ ~%}, {{~ ~}}, {#~ ~#})
* 1.38.4 (2019-03-23)
+8 -6
View File
@@ -1544,9 +1544,9 @@ function twig_include(Environment $env, $context, $template, $variables = [], $w
}
}
$result = '';
$loaded = null;
try {
$result = $env->resolveTemplate($template)->render($variables);
$loaded = $env->resolveTemplate($template);
} catch (LoaderError $e) {
if (!$ignoreMissing) {
if ($isSandboxed && !$alreadySandboxed) {
@@ -1569,11 +1569,13 @@ function twig_include(Environment $env, $context, $template, $variables = [], $w
throw $e;
}
if ($isSandboxed && !$alreadySandboxed) {
$sandbox->disableSandbox();
try {
return $loaded ? $loaded->render($variables) : '';
} finally {
if ($isSandboxed && !$alreadySandboxed) {
$sandbox->disableSandbox();
}
}
return $result;
}
/**
+29 -18
View File
@@ -37,43 +37,54 @@ class IncludeNode extends Node implements NodeOutputInterface
$compiler->addDebugInfo($this);
if ($this->getAttribute('ignore_missing')) {
$template = $compiler->getVarName();
$compiler
->write(sprintf("$%s = null;\n", $template))
->write("try {\n")
->indent()
->write(sprintf('$%s = ', $template))
;
}
$this->addGetTemplate($compiler);
$this->addGetTemplate($compiler);
$compiler->raw('->display(');
$this->addTemplateArguments($compiler);
$compiler->raw(");\n");
if ($this->getAttribute('ignore_missing')) {
$compiler
->raw(";\n")
->outdent()
->write("} catch (LoaderError \$e) {\n")
->indent()
->write("// ignore missing template\n")
->outdent()
->write("}\n\n")
->write("}\n")
->write(sprintf("if ($%s) {\n", $template))
->indent()
->write(sprintf('$%s->display(', $template))
;
$this->addTemplateArguments($compiler);
$compiler
->raw(");\n")
->outdent()
->write("}\n")
;
} else {
$this->addGetTemplate($compiler);
$compiler->raw('->display(');
$this->addTemplateArguments($compiler);
$compiler->raw(");\n");
}
}
protected function addGetTemplate(Compiler $compiler)
{
$compiler
->write('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
->write('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
}
protected function addTemplateArguments(Compiler $compiler)
@@ -0,0 +1,11 @@
--TEST--
"include" function
--TEMPLATE--
{{ include("included.twig", ignore_missing = true) }}
NOT DISPLAYED
--TEMPLATE(included.twig)--
{{ include("DOES NOT EXIST") }}
--DATA--
return []
--EXCEPTION--
Twig\Error\LoaderError: Template "DOES NOT EXIST" is not defined in "included.twig" at line 2.
@@ -0,0 +1,11 @@
--TEST--
"include" tag
--TEMPLATE--
{% include "included.twig" ignore missing %}
NOT DISPLAYED
--TEMPLATE(included.twig)--
{% include "DOES NOT EXIST" %}
--DATA--
return []
--EXCEPTION--
Twig\Error\LoaderError: Template "DOES NOT EXIST" is not defined in "included.twig" at line 2.
+6 -2
View File
@@ -76,13 +76,17 @@ EOF
$node = new IncludeNode($expr, $vars, true, true, 1);
$tests[] = [$node, <<<EOF
// line 1
\$__internal_%s = null;
try {
\$this->loadTemplate("foo.twig", null, 1)->display(twig_to_array(["foo" => true]));
\$__internal_%s = \$this->loadTemplate("foo.twig", null, 1);
} catch (LoaderError \$e) {
// ignore missing template
}
if (\$__internal_%s) {
\$__internal_%s->display(twig_to_array(["foo" => true]));
}
EOF
];
, null, true];
return $tests;
}