From f93a857126adb09a29d45b51779709f0f6669a3f Mon Sep 17 00:00:00 2001 From: Martin Hason Date: Wed, 2 Feb 2011 14:41:20 +0100 Subject: [PATCH] added possibility to customize "display" in the base template class --- lib/Twig/Node/Module.php | 2 +- lib/Twig/Template.php | 19 +++++++++++++++++++ lib/Twig/TemplateInterface.php | 3 ++- test/Twig/Tests/Node/ModuleTest.php | 6 +++--- test/Twig/Tests/Node/SandboxedModuleTest.php | 4 ++-- test/Twig/Tests/TemplateTest.php | 2 +- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 111f029..e8d8913 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -164,7 +164,7 @@ class Twig_Node_Module extends Twig_Node protected function compileDisplayHeader(Twig_Compiler $compiler) { $compiler - ->write("public function display(array \$context, array \$blocks = array())\n", "{\n") + ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n") ->indent() ; } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index e53cf96..99f8dfb 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -157,6 +157,17 @@ abstract class Twig_Template implements Twig_TemplateInterface } /** + * Displays the template with the given context. + * + * @param array $context An array of parameters to pass to the template + * @param array $blocks An array of blocks to pass to the template + */ + public function display(array $context, array $blocks = array()) + { + $this->doDisplay($context, $blocks); + } + + /** * Renders the template with the given context and returns it as string. * * @param array $context An array of parameters to pass to the template @@ -184,6 +195,14 @@ abstract class Twig_Template implements Twig_TemplateInterface } /** + * Auto-generated method to display the template with the given context. + * + * @param array $context An array of parameters to pass to the template + * @param array $blocks An array of blocks to pass to the template + */ + abstract protected function doDisplay(array $context, array $blocks = array()); + + /** * Returns a variable from the context. * * @param array $context The context diff --git a/lib/Twig/TemplateInterface.php b/lib/Twig/TemplateInterface.php index b3101d9..e5fb8f6 100644 --- a/lib/Twig/TemplateInterface.php +++ b/lib/Twig/TemplateInterface.php @@ -34,8 +34,9 @@ interface Twig_TemplateInterface * Displays the template with the given context. * * @param array $context An array of parameters to pass to the template + * @param array $blocks An array of blocks to pass to the template */ - function display(array $context); + function display(array $context, array $blocks = array()); /** * Returns the bound environment for this template. diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 194bed0..a5773a2 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -67,7 +67,7 @@ class Twig_Tests_Node_ModuleTest extends Twig_Tests_Node_TestCase /* foo.twig */ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template { - public function display(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = array()) { \$context = array_merge(\$this->env->getGlobals(), \$context); @@ -105,7 +105,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template return \$this->parent; } - public function display(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = array()) { \$context = array_merge(\$this->env->getGlobals(), \$context); @@ -150,7 +150,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template return \$this->parent; } - public function display(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = array()) { \$context = array_merge(\$this->env->getGlobals(), \$context); diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 209b2de..ada4bdf 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -65,7 +65,7 @@ class Twig_Tests_Node_SandboxedModuleTest extends Twig_Tests_Node_TestCase /* foo.twig */ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template { - public function display(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = array()) { \$this->checkSecurity(); \$context = array_merge(\$this->env->getGlobals(), \$context); @@ -115,7 +115,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template return \$this->parent; } - public function display(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = array()) { \$context = array_merge(\$this->env->getGlobals(), \$context); diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index c99a1e1..3700cb2 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -91,7 +91,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase class Twig_TemplateTest extends Twig_Template { - public function display(array $context) + protected function doDisplay(array $context, array $blocks = array()) { } -- 1.7.2.5