浏览代码

[Twig][Form] Optimize form rendering

Victor Berchet 14 年之前
父节点
当前提交
eb10c66a55

+ 39 - 23
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -33,6 +33,8 @@ class FormExtension extends \Twig_Extension
     {
         $this->themes = new \SplObjectStorage();
         $this->varStack = new \SplObjectStorage();
+        $this->templates = new \SplObjectStorage();
+
         $this->resources = $resources;
     }
 
@@ -53,6 +55,7 @@ class FormExtension extends \Twig_Extension
     public function setTheme(FormView $view, array $resources)
     {
         $this->themes->attach($view, $resources);
+        $this->templates->detach($view);
     }
 
     /**
@@ -183,35 +186,48 @@ class FormExtension extends \Twig_Extension
         throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks)));
     }
 
+    /**
+     * Returns the templates used by the view.
+     *
+     * templates are looked for in the following resources:
+     *   * resources from the themes (and its parents)
+     *   * default resources
+     *
+     * @param FormView $view The view
+     *
+     * @return array An array of Twig_TemplateInterface instances
+     */
     protected function getTemplates(FormView $view)
     {
-        // templates are looked for in the following resources:
-        //   * resources from the themes (and its parents)
-        //   * default resources
-
-        // defaults
-        $all = $this->resources;
-
-        // themes
-        $parent = $view;
-        do {
-            if (isset($this->themes[$parent])) {
-                $all = array_merge($all, $this->themes[$parent]);
-            }
-        } while ($parent = $parent->getParent());
+        if (!$this->templates->contains($view)) {
+            // defaults
+            $all = $this->resources;
+
+            // themes
+            $parent = $view;
+            do {
+                if (isset($this->themes[$parent])) {
+                    $all = array_merge($all, $this->themes[$parent]);
+                }
+            } while ($parent = $parent->getParent());
 
-        $templates = array();
-        foreach ($all as $resource) {
-            if (!$resource instanceof \Twig_Template) {
-                $resource = $this->environment->loadTemplate($resource);
-            }
+            $templates = array();
+            foreach ($all as $resource) {
+                if (!$resource instanceof \Twig_Template) {
+                    $resource = $this->environment->loadTemplate($resource);
+                }
+
+                $blocks = array();
+                foreach ($this->getBlockNames($resource) as $name) {
+                    $blocks[$name] = $resource;
+                }
 
-            $blocks = array();
-            foreach ($this->getBlockNames($resource) as $name) {
-                $blocks[$name] = $resource;
+                $templates = array_replace($templates, $blocks);
             }
 
-            $templates = array_replace($templates, $blocks);
+            $this->templates->attach($view, $templates);
+        } else {
+            $templates = $this->templates[$view];
         }
 
         return $templates;

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -156,7 +156,7 @@ class FormHelper extends Helper
             $template = $this->templateDir.':'.$template;
         }
 */
-$template = 'FrameworkBundle:Form:'.$template;
+        $template = 'FrameworkBundle:Form:'.$template;
         if (!$this->engine->exists($template)) {
             $template = false;
         }