ソースを参照

Merge remote branch 'vicb/twig-theme-inheritance'

* vicb/twig-theme-inheritance:
  [Form] Further tweaks of the twig theme inheritance
  [Form] Fix twig theme inheritance
Fabien Potencier 14 年 前
コミット
0a7ce63d8f

+ 8 - 11
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -249,8 +249,9 @@ class FormExtension extends \Twig_Extension
     /**
      * Returns the templates used by the view.
      *
-     * templates are looked for in the following resources:
+     * Templates are looked for in the resources in the following order:
      *   * resources from the themes (and its parents)
+     *   * resources from the themes of parent views (up to the root view)
      *   * default resources
      *
      * @param FormView $view The view
@@ -260,29 +261,25 @@ class FormExtension extends \Twig_Extension
     protected function getTemplates(FormView $view)
     {
         if (!$this->templates->contains($view)) {
-            // defaults
-            $all = $this->resources;
-
-            // themes
+            $resources = array();
             $parent = $view;
             do {
                 if (isset($this->themes[$parent])) {
-                    $all = array_merge($all, $this->themes[$parent]);
+                    $resources = array_merge($this->themes[$parent], $resources);
                 }
             } while ($parent = $parent->getParent());
 
+            $resources = array_merge($this->resources, $resources);
+
             $templates = array();
-            foreach ($all as $resource) {
+            foreach ($resources as $resource) {
                 if (!$resource instanceof \Twig_Template) {
                     $resource = $this->environment->loadTemplate($resource);
                 }
 
-                $blocks = array();
                 foreach ($this->getBlockNames($resource) as $name) {
-                    $blocks[$name] = $resource;
+                    $templates[$name] = $resource;
                 }
-
-                $templates = array_replace($templates, $blocks);
             }
 
             $this->templates->attach($view, $templates);

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

@@ -48,6 +48,48 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
         $this->extension->initRuntime($environment);
     }
 
+    public function testThemeInheritance()
+    {
+        $child = $this->factory->createNamedBuilder('form', 'child')
+            ->add('field', 'text')
+            ->getForm();
+
+        $view = $this->factory->createNamedBuilder('form', 'parent')
+            ->add('field', 'text')
+            ->getForm()
+            ->add($child)
+            ->createView()
+        ;
+
+        $this->extension->setTheme($view, array('parent_label.html.twig'));
+        $this->extension->setTheme($view['child'], array('child_label.html.twig'));
+
+        $this->assertWidgetMatchesXpath($view, array(),
+'/div
+    [
+        ./input[@type="hidden"]
+        /following-sibling::div
+            [
+                ./label[.="parent"]
+                /following-sibling::input[@type="text"]
+            ]
+        /following-sibling::div
+            [
+                ./label
+                /following-sibling::div
+                    [
+                        ./div
+                            [
+                                ./label[.="child"]
+                                /following-sibling::input[@type="text"]
+                            ]
+                    ]
+            ]
+    ]
+'
+        );
+    }
+
     protected function renderEnctype(FormView $view)
     {
         return (string)$this->extension->renderEnctype($view);

+ 3 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/child_label.html.twig

@@ -0,0 +1,3 @@
+{% block field_label %}
+    <label>child</label>
+{% endblock field_label %}

+ 3 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/parent_label.html.twig

@@ -0,0 +1,3 @@
+{% block field_label %}
+    <label>parent</label>
+{% endblock field_label %}