Browse code

fixed render block with template wrapper

Fabien Potencier authored on 17/11/2016 15:26:37
Showing 2 changed files
... ...
@@ -88,20 +88,6 @@ final class Twig_TemplateWrapper
88 88
      */
89 89
     public function renderBlock($name, $context = array())
90 90
     {
91
-        ob_start();
92
-        $this->displayBlock($name, $context);
93
-
94
-        return ob_get_clean();
95
-    }
96
-
97
-    /**
98
-     * Displays a template block.
99
-     *
100
-     * @param string $name    The block name to render
101
-     * @param array  $context An array of parameters to pass to the template
102
-     */
103
-    public function displayBlock($name, $context = array())
104
-    {
105 91
         $context = $this->env->mergeGlobals($context);
106 92
         $level = ob_get_level();
107 93
         ob_start();
... ...
@@ -125,6 +111,17 @@ final class Twig_TemplateWrapper
125 111
     }
126 112
 
127 113
     /**
114
+     * Displays a template block.
115
+     *
116
+     * @param string $name    The block name to render
117
+     * @param array  $context An array of parameters to pass to the template
118
+     */
119
+    public function displayBlock($name, $context = array())
120
+    {
121
+        $this->template->displayBlock($name, $this->env->mergeGlobals($context));
122
+    }
123
+
124
+    /**
128 125
      * @return Twig_Source
129 126
      */
130 127
     public function getSourceContext()
... ...
@@ -35,4 +35,30 @@ class Twig_Tests_TemplateWrapperTest extends PHPUnit_Framework_TestCase
35 35
         $this->assertTrue($wrapper->hasBlock('extended'));
36 36
         $this->assertEquals(array('foo', 'extended'), $wrapper->getBlockNames());
37 37
     }
38
+
39
+    public function testRenderBlock()
40
+    {
41
+        $twig = new Twig_Environment(new Twig_Loader_Array(array(
42
+            'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}',
43
+        )));
44
+        $twig->addGlobal('bar', 'BAR');
45
+
46
+        $wrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index'));
47
+        $this->assertEquals('FOOBAR', $wrapper->renderBlock('foo', array('foo' => 'FOO')));
48
+    }
49
+
50
+    public function testDisplayBlock()
51
+    {
52
+        $twig = new Twig_Environment(new Twig_Loader_Array(array(
53
+            'index' => '{% block foo %}{{ foo }}{{ bar }}{% endblock %}',
54
+        )));
55
+        $twig->addGlobal('bar', 'BAR');
56
+
57
+        $wrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index'));
58
+
59
+        ob_start();
60
+        $wrapper->displayBlock('foo', array('foo' => 'FOO'));
61
+
62
+        $this->assertEquals('FOOBAR', ob_get_clean());
63
+    }
38 64
 }