mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 12:37:15 +00:00
fixed "include" with "ignore missing" when an error loading occurs in the included template (again)
This commit is contained in:
+1
-17
@@ -83,7 +83,6 @@ class Environment
|
|||||||
private $runtimeLoaders = [];
|
private $runtimeLoaders = [];
|
||||||
private $runtimes = [];
|
private $runtimes = [];
|
||||||
private $optionsHash;
|
private $optionsHash;
|
||||||
private $loading = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@@ -507,22 +506,7 @@ class Environment
|
|||||||
$this->initRuntime();
|
$this->initRuntime();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->loading[$cls])) {
|
return $this->loadedTemplates[$cls] = new $cls($this);
|
||||||
throw new RuntimeError(sprintf('Circular reference detected for Twig template "%s", path: %s.', $name, implode(' -> ', array_merge($this->loading, [$name]))));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->loading[$cls] = $name;
|
|
||||||
|
|
||||||
try {
|
|
||||||
$this->loadedTemplates[$cls] = new $cls($this);
|
|
||||||
unset($this->loading[$cls]);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
unset($this->loading[$cls]);
|
|
||||||
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->loadedTemplates[$cls];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1570,12 +1570,20 @@ function twig_include(Environment $env, $context, $template, $variables = [], $w
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $loaded ? $loaded->render($variables) : '';
|
$ret = $loaded ? $loaded->render($variables) : '';
|
||||||
} finally {
|
} catch (\Exception $e) {
|
||||||
if ($isSandboxed && !$alreadySandboxed) {
|
if ($isSandboxed && !$alreadySandboxed) {
|
||||||
$sandbox->disableSandbox();
|
$sandbox->disableSandbox();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($isSandboxed && !$alreadySandboxed) {
|
||||||
|
$sandbox->disableSandbox();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+10
-11
@@ -198,17 +198,6 @@ class ModuleNode extends Node
|
|||||||
// parent
|
// parent
|
||||||
if (!$this->hasNode('parent')) {
|
if (!$this->hasNode('parent')) {
|
||||||
$compiler->write("\$this->parent = false;\n\n");
|
$compiler->write("\$this->parent = false;\n\n");
|
||||||
} elseif (($parent = $this->getNode('parent')) && $parent instanceof ConstantExpression) {
|
|
||||||
$compiler
|
|
||||||
->addDebugInfo($parent)
|
|
||||||
->write('$this->parent = $this->loadTemplate(')
|
|
||||||
->subcompile($parent)
|
|
||||||
->raw(', ')
|
|
||||||
->repr($this->source->getName())
|
|
||||||
->raw(', ')
|
|
||||||
->repr($parent->getTemplateLine())
|
|
||||||
->raw(");\n")
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$countTraits = \count($this->getNode('traits'));
|
$countTraits = \count($this->getNode('traits'));
|
||||||
@@ -331,8 +320,18 @@ class ModuleNode extends Node
|
|||||||
|
|
||||||
if ($this->hasNode('parent')) {
|
if ($this->hasNode('parent')) {
|
||||||
$parent = $this->getNode('parent');
|
$parent = $this->getNode('parent');
|
||||||
|
|
||||||
$compiler->addDebugInfo($parent);
|
$compiler->addDebugInfo($parent);
|
||||||
if ($parent instanceof ConstantExpression) {
|
if ($parent instanceof ConstantExpression) {
|
||||||
|
$compiler
|
||||||
|
->write('$this->parent = $this->loadTemplate(')
|
||||||
|
->subcompile($parent)
|
||||||
|
->raw(', ')
|
||||||
|
->repr($this->source->getName())
|
||||||
|
->raw(', ')
|
||||||
|
->repr($parent->getTemplateLine())
|
||||||
|
->raw(");\n")
|
||||||
|
;
|
||||||
$compiler->write('$this->parent');
|
$compiler->write('$this->parent');
|
||||||
} else {
|
} else {
|
||||||
$compiler->write('$this->getParent($context)');
|
$compiler->write('$this->getParent($context)');
|
||||||
|
|||||||
@@ -498,33 +498,6 @@ EOF
|
|||||||
$this->assertEquals('foo', $twig->render('func_string_named_args'));
|
$this->assertEquals('foo', $twig->render('func_string_named_args'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Twig\Error\RuntimeError
|
|
||||||
* @expectedExceptionMessage Circular reference detected for Twig template "base.html.twig", path: base.html.twig -> base.html.twig in "base.html.twig" at line 1
|
|
||||||
*/
|
|
||||||
public function testFailLoadTemplateOnCircularReference()
|
|
||||||
{
|
|
||||||
$twig = new Environment(new ArrayLoader([
|
|
||||||
'base.html.twig' => '{% extends "base.html.twig" %}',
|
|
||||||
]));
|
|
||||||
|
|
||||||
$twig->load('base.html.twig');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Twig\Error\RuntimeError
|
|
||||||
* @expectedExceptionMessage Circular reference detected for Twig template "base1.html.twig", path: base1.html.twig -> base2.html.twig -> base1.html.twig in "base1.html.twig" at line 1
|
|
||||||
*/
|
|
||||||
public function testFailLoadTemplateOnComplexCircularReference()
|
|
||||||
{
|
|
||||||
$twig = new Environment(new ArrayLoader([
|
|
||||||
'base1.html.twig' => '{% extends "base2.html.twig" %}',
|
|
||||||
'base2.html.twig' => '{% extends "base1.html.twig" %}',
|
|
||||||
]));
|
|
||||||
|
|
||||||
$twig->load('base1.html.twig');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getMockLoader($templateName, $templateContent)
|
protected function getMockLoader($templateName, $templateContent)
|
||||||
{
|
{
|
||||||
// to be removed in 2.0
|
// to be removed in 2.0
|
||||||
|
|||||||
@@ -4,5 +4,7 @@ Exception for an undefined parent
|
|||||||
{% extends 'foo.html' %}
|
{% extends 'foo.html' %}
|
||||||
|
|
||||||
{% set foo = "foo" %}
|
{% set foo = "foo" %}
|
||||||
|
--DATA--
|
||||||
|
return []
|
||||||
--EXCEPTION--
|
--EXCEPTION--
|
||||||
Twig\Error\LoaderError: Template "foo.html" is not defined in "index.twig" at line 2.
|
Twig\Error\LoaderError: Template "foo.html" is not defined in "index.twig" at line 2.
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ version_compare(phpversion(), '7.0.0', '>=')
|
|||||||
{{ val2 is same as (0.0) ? 'Yes' : 'No' }}
|
{{ val2 is same as (0.0) ? 'Yes' : 'No' }}
|
||||||
{{ val is same as (val2) ? 'Yes' : 'No' }}
|
{{ val is same as (val2) ? 'Yes' : 'No' }}
|
||||||
--DATA--
|
--DATA--
|
||||||
return array('val' => 0.0)
|
return ['val' => 0.0]
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
Yes
|
Yes
|
||||||
Yes
|
Yes
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
--TEST--
|
||||||
|
"include" function
|
||||||
|
--TEMPLATE--
|
||||||
|
{{ include(['bad.twig', 'good.twig'], ignore_missing = true) }}
|
||||||
|
NOT DISPLAYED
|
||||||
|
--TEMPLATE(bad.twig)--
|
||||||
|
{% extends 'DOES NOT EXIST' %}
|
||||||
|
--TEMPLATE(good.twig)--
|
||||||
|
NOT DISPLAYED
|
||||||
|
--DATA--
|
||||||
|
return []
|
||||||
|
--EXCEPTION--
|
||||||
|
Twig\Error\LoaderError: Template "DOES NOT EXIST" is not defined in "bad.twig" at line 2.
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
--TEST--
|
||||||
|
"include" tag
|
||||||
|
--TEMPLATE--
|
||||||
|
{% include ['bad.twig', 'good.twig'] ignore missing %}
|
||||||
|
NOT DISPLAYED
|
||||||
|
--TEMPLATE(bad.twig)--
|
||||||
|
{% extends 'DOES NOT EXIST' %}
|
||||||
|
--TEMPLATE(good.twig)--
|
||||||
|
NOT DISPLAYED
|
||||||
|
--DATA--
|
||||||
|
return []
|
||||||
|
--EXCEPTION--
|
||||||
|
Twig\Error\LoaderError: Template "DOES NOT EXIST" is not defined in "bad.twig" at line 2.
|
||||||
@@ -140,14 +140,13 @@ class __TwigTemplate_%x extends \Twig\Template
|
|||||||
{
|
{
|
||||||
parent::__construct(\$env);
|
parent::__construct(\$env);
|
||||||
|
|
||||||
// line 1
|
|
||||||
\$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
|
|
||||||
\$this->blocks = [
|
\$this->blocks = [
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function doGetParent(array \$context)
|
protected function doGetParent(array \$context)
|
||||||
{
|
{
|
||||||
|
// line 1
|
||||||
return "layout.twig";
|
return "layout.twig";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,6 +155,7 @@ class __TwigTemplate_%x extends \Twig\Template
|
|||||||
// line 2
|
// line 2
|
||||||
\$context["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2);
|
\$context["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2);
|
||||||
// line 1
|
// line 1
|
||||||
|
\$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
|
||||||
\$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
|
\$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +171,7 @@ class __TwigTemplate_%x extends \Twig\Template
|
|||||||
|
|
||||||
public function getDebugInfo()
|
public function getDebugInfo()
|
||||||
{
|
{
|
||||||
return array ( 37 => 1, 35 => 2, 22 => 1,);
|
return array ( 36 => 1, 34 => 2, 28 => 1,);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */
|
/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */
|
||||||
|
|||||||
Reference in New Issue
Block a user