Преглед на файлове

[Form] The TwigTheme now accepts several templates

Bernhard Schussek преди 14 години
родител
ревизия
0f8cd43ac4
променени са 2 файла, в които са добавени 19 реда и са изтрити 19 реда
  1. 1 1
      src/Symfony/Component/Form/Renderer/Theme/ThemeInterface.php
  2. 18 18
      src/Symfony/Component/Form/Renderer/Theme/TwigTheme.php

+ 1 - 1
src/Symfony/Component/Form/Renderer/Theme/ThemeInterface.php

@@ -13,5 +13,5 @@ namespace Symfony\Component\Form\Renderer\Theme;
 
 interface ThemeInterface
 {
-    function render($template, $block, array $parameters);
+    function render($field, $section, array $parameters);
 }

+ 18 - 18
src/Symfony/Component/Form/Renderer/Theme/TwigTheme.php

@@ -18,26 +18,26 @@ use Symfony\Component\Form\Exception\FormException;
 class TwigTheme implements ThemeInterface
 {
     private $environment;
-    private $template;
-    private $blocks;
+    private $templates;
+    private $templatesByBlock;
 
-    public function __construct(\Twig_Environment $environment, $template)
+    public function __construct(\Twig_Environment $environment, $templates)
     {
         $this->environment = $environment;
-        $this->template = $template;
+        $this->templates = (array)$templates;
     }
 
     private function initialize()
     {
-        if (!$this->blocks) {
-            $this->blocks = array();
+        if (!$this->templatesByBlock) {
+            $this->templatesByBlock = array();
 
-            if (!$this->template instanceof \Twig_Template) {
-                $this->template = $this->environment->loadTemplate($this->template);
-            }
+            foreach ($this->templates as $template) {
+                $template = $this->environment->loadTemplate($template);
 
-            foreach ($this->getBlockNames($this->template) as $blockName) {
-                $this->blocks[$blockName] = true;
+                foreach ($this->getBlockNames($template) as $blockName) {
+                    $this->templatesByBlock[$blockName] = $template;
+                }
             }
         }
     }
@@ -54,18 +54,18 @@ class TwigTheme implements ThemeInterface
         return array_unique($names);
     }
 
-    public function render($template, $block, array $parameters)
+    public function render($field, $section, array $parameters)
     {
         $this->initialize();
 
-        if (isset($this->blocks[$template.'__'.$block])) {
-            $blockName = $template.'__'.$block;
-        } else if (isset($this->blocks[$block])) {
-            $blockName = $block;
+        if (isset($this->templatesByBlock[$field.'__'.$section])) {
+            $blockName = $field.'__'.$section;
+        } else if (isset($this->templatesByBlock[$section])) {
+            $blockName = $section;
         } else {
-            throw new FormException(sprintf('The form theme is missing the "%s" block', $block));
+            throw new FormException(sprintf('The form theme is missing the "%s" block', $section));
         }
 
-        return $this->template->renderBlock($blockName, $parameters);
+        return $this->templatesByBlock[$blockName]->renderBlock($blockName, $parameters);
     }
 }