mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 02:46:29 +00:00
moved Template::getParent() logic to the base class
This commit is contained in:
@@ -62,28 +62,20 @@ class Twig_Node_Module extends Twig_Node
|
||||
|
||||
protected function compileGetParent(Twig_Compiler $compiler)
|
||||
{
|
||||
$compiler
|
||||
->write("protected function doGetParent(array \$context)\n", "{\n")
|
||||
->indent()
|
||||
->write("return ")
|
||||
;
|
||||
|
||||
if (null === $this->getNode('parent')) {
|
||||
return;
|
||||
$compiler->raw("false");
|
||||
} else {
|
||||
$compiler->subcompile($this->getNode('parent'));
|
||||
}
|
||||
|
||||
$compiler
|
||||
->write("public function getParent(array \$context)\n", "{\n")
|
||||
->indent()
|
||||
->write("\$parent = ")
|
||||
->subcompile($this->getNode('parent'))
|
||||
->raw(";\n")
|
||||
->write("if (\$parent instanceof Twig_Template) {\n")
|
||||
->indent()
|
||||
->write("\$name = \$parent->getTemplateName();\n")
|
||||
->write("\$this->parent[\$name] = \$parent;\n")
|
||||
->write("\$parent = \$name;\n")
|
||||
->outdent()
|
||||
->write("} elseif (!isset(\$this->parent[\$parent])) {\n")
|
||||
->indent()
|
||||
->write("\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);\n")
|
||||
->outdent()
|
||||
->write("}\n\n")
|
||||
->write("return \$this->parent[\$parent];\n")
|
||||
->outdent()
|
||||
->write("}\n\n")
|
||||
;
|
||||
|
||||
+14
-1
@@ -61,9 +61,22 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
*/
|
||||
public function getParent(array $context)
|
||||
{
|
||||
return false;
|
||||
$parent = $this->doGetParent($context);
|
||||
if (false === $parent) {
|
||||
return false;
|
||||
} elseif ($parent instanceof Twig_Template) {
|
||||
$name = $parent->getTemplateName();
|
||||
$this->parent[$name] = $parent;
|
||||
$parent = $name;
|
||||
} elseif (!isset($this->parent[$parent])) {
|
||||
$this->parent[$parent] = $this->env->loadTemplate($parent);
|
||||
}
|
||||
|
||||
return $this->parent[$parent];
|
||||
}
|
||||
|
||||
abstract protected function doGetParent(array $context);
|
||||
|
||||
/**
|
||||
* Displays a parent block.
|
||||
*
|
||||
|
||||
@@ -69,6 +69,11 @@ class Twig_Tests_Node_ModuleTest extends Twig_Tests_Node_TestCase
|
||||
/* foo.twig */
|
||||
class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
|
||||
{
|
||||
protected function doGetParent(array \$context)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = array())
|
||||
{
|
||||
\$context = array_merge(\$this->env->getGlobals(), \$context);
|
||||
@@ -103,18 +108,9 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
|
||||
{
|
||||
protected \$parent;
|
||||
|
||||
public function getParent(array \$context)
|
||||
protected function doGetParent(array \$context)
|
||||
{
|
||||
\$parent = "layout.twig";
|
||||
if (\$parent instanceof Twig_Template) {
|
||||
\$name = \$parent->getTemplateName();
|
||||
\$this->parent[\$name] = \$parent;
|
||||
\$parent = \$name;
|
||||
} elseif (!isset(\$this->parent[\$parent])) {
|
||||
\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
|
||||
}
|
||||
|
||||
return \$this->parent[\$parent];
|
||||
return "layout.twig";
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = array())
|
||||
@@ -155,18 +151,9 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
|
||||
{
|
||||
protected \$parent;
|
||||
|
||||
public function getParent(array \$context)
|
||||
protected function doGetParent(array \$context)
|
||||
{
|
||||
\$parent = ((true) ? ("foo") : ("foo"));
|
||||
if (\$parent instanceof Twig_Template) {
|
||||
\$name = \$parent->getTemplateName();
|
||||
\$this->parent[\$name] = \$parent;
|
||||
\$parent = \$name;
|
||||
} elseif (!isset(\$this->parent[\$parent])) {
|
||||
\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
|
||||
}
|
||||
|
||||
return \$this->parent[\$parent];
|
||||
return ((true) ? ("foo") : ("foo"));
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = array())
|
||||
|
||||
@@ -67,6 +67,11 @@ class Twig_Tests_Node_SandboxedModuleTest extends Twig_Tests_Node_TestCase
|
||||
/* foo.twig */
|
||||
class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
|
||||
{
|
||||
protected function doGetParent(array \$context)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = array())
|
||||
{
|
||||
\$this->checkSecurity();
|
||||
@@ -114,18 +119,9 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
|
||||
{
|
||||
protected \$parent;
|
||||
|
||||
public function getParent(array \$context)
|
||||
protected function doGetParent(array \$context)
|
||||
{
|
||||
\$parent = "layout.twig";
|
||||
if (\$parent instanceof Twig_Template) {
|
||||
\$name = \$parent->getTemplateName();
|
||||
\$this->parent[\$name] = \$parent;
|
||||
\$parent = \$name;
|
||||
} elseif (!isset(\$this->parent[\$parent])) {
|
||||
\$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
|
||||
}
|
||||
|
||||
return \$this->parent[\$parent];
|
||||
return "layout.twig";
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = array())
|
||||
|
||||
@@ -130,6 +130,10 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
class Twig_TemplateTest extends Twig_Template
|
||||
{
|
||||
protected function doGetParent(array $context)
|
||||
{
|
||||
}
|
||||
|
||||
protected function doDisplay(array $context, array $blocks = array())
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user