Ver Fonte

[Form][TwigBridge] Fix rendering

Victor Berchet há 14 anos atrás
pai
commit
8677aa3dce

+ 32 - 26
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -217,13 +217,15 @@ class FormExtension extends \Twig_Extension
         }
 
         $templates = $this->getTemplates($view);
-        $blocks = $view->get('types');
-        array_unshift($blocks, '_'.$view->get('id'));
+        $template = end($templates);
+        $blocks = $this->getBlocks($templates);
+        $types = $view->get('types');
+        array_unshift($types, '_'.$view->get('id'));
 
-        foreach ($blocks as &$block) {
-            $block = $block.'_'.$section;
+        foreach ($types as $type) {
+            $block = $type.'_'.$section;
 
-            if (isset($templates[$block])) {
+            if (isset($blocks[$block])) {
 
                 $this->varStack[$view] = array_replace(
                     $view->all(),
@@ -231,7 +233,7 @@ class FormExtension extends \Twig_Extension
                     $variables
                 );
 
-                $html = $templates[$block]->renderBlock($block, $this->varStack[$view]);
+                $html = $template->renderBlock($block, $this->varStack[$view], $blocks);
 
                 if ($mainTemplate) {
                     $view->setRendered();
@@ -261,25 +263,21 @@ class FormExtension extends \Twig_Extension
     protected function getTemplates(FormView $view)
     {
         if (!$this->templates->contains($view)) {
-            $resources = array();
+            $templates = array();
             $parent = $view;
             do {
                 if (isset($this->themes[$parent])) {
-                    $resources = array_merge($this->themes[$parent], $resources);
+                    $templates = array_merge($this->themes[$parent], $templates);
                 }
             } while ($parent = $parent->getParent());
 
-            $resources = array_merge($this->resources, $resources);
+            $templates = array_merge($this->resources, $templates);
 
-            $templates = array();
-            foreach ($resources as $resource) {
-                if (!$resource instanceof \Twig_Template) {
-                    $resource = $this->environment->loadTemplate($resource);
-                }
-
-                foreach ($this->getBlockNames($resource) as $name) {
-                    $templates[$name] = $resource;
+            foreach ($templates as $i => $template) {
+                if (!$template instanceof \Twig_Template) {
+                    $template = $this->environment->loadTemplate($template);
                 }
+                $templates[$i] = $template;
             }
 
             $this->templates->attach($view, $templates);
@@ -291,20 +289,28 @@ class FormExtension extends \Twig_Extension
     }
 
     /**
-     * Returns all the block defined in the template hierarchy.
+     * Returns the blocks to be used to render the template stack.
      *
-     * @param \Twig_Template $template
+     * @param \ArrayObject $templates The template stack
      *
-     * @return array A list of block names
+     * @return array A list of block to be used to render the view
      */
-    protected function getBlockNames(\Twig_Template $template)
+    protected function getBlocks(array $templates)
     {
-        $names = array();
-        do {
-            $names = array_merge($names, $template->getBlockNames());
-        } while (false !== $template = $template->getParent(array()));
+        $blocks = array();
+
+        foreach ($templates as $template) {
+
+            $templateBlocks = array();
+
+            do {
+                $templateBlocks = array_merge($template->getBlocks(), $templateBlocks);
+            } while (false !== $template = $template->getParent(array()));
+
+            $blocks = array_merge($blocks, $templateBlocks);
+        }
 
-        return array_unique($names);
+        return $blocks;
     }
 
     /**

+ 45 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

@@ -48,6 +48,51 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
         $this->extension->initRuntime($environment);
     }
 
+    public function testThemeBlockInheritance()
+    {
+        $view = $this->factory
+            ->createNamed('email', 'name')
+            ->createView()
+        ;
+
+        $this->extension->setTheme($view, array('theme.html.twig'));
+
+        $this->assertMatchesXpath(
+            $this->renderWidget($view),
+            '/input[@type="email"][@rel="theme"]'
+        );
+    }
+
+    public function testThemeBlockInheritanceUsingUse()
+    {
+        $view = $this->factory
+            ->createNamed('email', 'name')
+            ->createView()
+        ;
+
+        $this->extension->setTheme($view, array('theme_use.html.twig'));
+
+        $this->assertMatchesXpath(
+            $this->renderWidget($view),
+            '/input[@type="email"][@rel="theme"]'
+        );
+    }
+
+    public function testThemeBlockInheritanceUsingExtend()
+    {
+        $view = $this->factory
+            ->createNamed('email', 'name')
+            ->createView()
+        ;
+
+        $this->extension->setTheme($view, array('theme_extends.html.twig'));
+
+        $this->assertMatchesXpath(
+            $this->renderWidget($view),
+            '/input[@type="email"][@rel="theme"]'
+        );
+    }
+
     public function testThemeInheritance()
     {
         $child = $this->factory->createNamedBuilder('form', 'child')

+ 6 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/theme.html.twig

@@ -0,0 +1,6 @@
+{% block field_widget %}
+{% spaceless %}
+    {% set type = type|default('text') %}
+    <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
+{% endspaceless %}
+{% endblock field_widget %}

+ 8 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/theme_extends.html.twig

@@ -0,0 +1,8 @@
+{% extends 'div_layout.html.twig' %}
+
+{% block field_widget %}
+{% spaceless %}
+    {% set type = type|default('text') %}
+    <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
+{% endspaceless %}
+{% endblock field_widget %}

+ 8 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/theme_use.html.twig

@@ -0,0 +1,8 @@
+{% use 'div_layout.html.twig' %}
+
+{% block field_widget %}
+{% spaceless %}
+    {% set type = type|default('text') %}
+    <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" rel="theme" />
+{% endspaceless %}
+{% endblock field_widget %}