From ed69d7fa353ae8434eb73c2543eec092ae389f66 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sat, 26 Mar 2011 18:38:03 +0100 Subject: [PATCH] [Form] Fixed rendering using the PHP Templating engine --- .../FrameworkBundle/Form/PhpEngineTheme.php | 29 ++++++++++++------- .../Form/PhpEngineThemeFactory.php | 12 ++++++-- .../FrameworkBundle/Resources/config/form.xml | 8 ++--- .../views/Form/field_enctype.html.php | 1 + ...{errors.html.php => field_errors.html.php} | 0 .../{label.html.php => field_label.html.php} | 0 .../{rest.html.php => field_rest.html.php} | 0 .../Form/{row.html.php => field_row.html.php} | 0 ...{widget.html.php => field_widget.html.php} | 0 .../Templating/Loader/FilesystemLoader.php | 6 ++-- 10 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php rename src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/{errors.html.php => field_errors.html.php} (100%) rename src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/{label.html.php => field_label.html.php} (100%) rename src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/{rest.html.php => field_rest.html.php} (100%) rename src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/{row.html.php => field_row.html.php} (100%) rename src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/{widget.html.php => field_widget.html.php} (100%) diff --git a/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineTheme.php b/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineTheme.php index cd54289ee6..321b4e5670 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineTheme.php +++ b/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineTheme.php @@ -37,26 +37,28 @@ class PhpEngineTheme implements FormThemeInterface /** * @var string */ - private $template; + private $templateDir; /** * @param PhpEngine $engine */ - public function __construct(PhpEngine $engine, $template = null) + public function __construct(PhpEngine $engine, $templateDir = null) { $this->engine = $engine; - $this->template = $template; + $this->templateDir = $templateDir; } - public function render($field, $section, array $parameters) + public function render(array $blocks, $section, array $parameters) { - if ($template = $this->lookupTemplate($field."_".$section)) { - return $this->engine->render($template, $parameters); - } else if ($template = $this->lookupTemplate($section)) { - return $this->engine->render($template, $parameters); - } else { - throw new FormException(sprintf('The form theme is missing the "%s" template file.', $section)); + foreach ($blocks as &$block) { + $block = $block.'_'.$section; + + if ($template = $this->lookupTemplate($block)) { + return $this->engine->render($template, $parameters); + } } + + throw new FormException(sprintf('The form theme is missing the "%s" template files', implode('", "', $blocks))); } protected function lookupTemplate($templateName) @@ -65,7 +67,12 @@ class PhpEngineTheme implements FormThemeInterface return self::$cache[$templateName]; } - $template = (($this->template) ? ($this->template.":") : "") . $templateName.'.html.php'; + $template = $templateName.'.html.php'; + + if ($this->templateDir) { + $template = $this->templateDir . ':' . $template; + } + if (!$this->engine->exists($template)) { $template = false; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineThemeFactory.php b/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineThemeFactory.php index af49731de6..660ad6e124 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineThemeFactory.php +++ b/src/Symfony/Bundle/FrameworkBundle/Form/PhpEngineThemeFactory.php @@ -26,16 +26,22 @@ class PhpEngineThemeFactory implements FormThemeFactoryInterface */ private $engine; - public function __construct(PhpEngine $engine) + /** + * @var string + */ + private $defaultTemplateDir; + + public function __construct(PhpEngine $engine, $defaultTemplateDir = null) { $this->engine = $engine; + $this->defaultTemplateDir = $defaultTemplateDir; } /** * @see Symfony\Component\Form\Renderer\Theme\FormThemeFactoryInterface::create() */ - public function create($template = null) + public function create($templateDir = null) { - return new PhpEngineTheme($this->engine, $template); + return new PhpEngineTheme($this->engine, $templateDir ?: $this->defaultTemplateDir); } } \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml index 7fec315d38..59bfce7881 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml @@ -16,7 +16,8 @@ Symfony\Component\Form\Renderer\Theme\TwigThemeFactory Symfony\Component\Form\Renderer\Theme\PhpThemeFactory Symfony\Bundle\FrameworkBundle\Form\PhpEngineThemeFactory - TwigBundle::div_layout.html.twig + TwigBundle::div_layout.html.twig + FrameworkBundle:Form Symfony\Component\Form\FormFactory true _token @@ -82,7 +83,7 @@ - %form.theme.template% + %form.theme.template.twig% @@ -91,6 +92,7 @@ + %form.theme.template.phpengine% @@ -129,8 +131,6 @@ - - %form.theme.template% diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php new file mode 100644 index 0000000000..18d8f555a2 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php @@ -0,0 +1 @@ +getVar('multipart')): ?>enctype="multipart/form-data" \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/errors.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php similarity index 100% rename from src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/errors.html.php rename to src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/label.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php similarity index 100% rename from src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/label.html.php rename to src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/rest.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rest.html.php similarity index 100% rename from src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/rest.html.php rename to src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rest.html.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/row.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php similarity index 100% rename from src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/row.html.php rename to src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_widget.html.php similarity index 100% rename from src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget.html.php rename to src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_widget.html.php diff --git a/src/Symfony/Component/Templating/Loader/FilesystemLoader.php b/src/Symfony/Component/Templating/Loader/FilesystemLoader.php index f5d35d196b..3007a0e4f0 100644 --- a/src/Symfony/Component/Templating/Loader/FilesystemLoader.php +++ b/src/Symfony/Component/Templating/Loader/FilesystemLoader.php @@ -106,9 +106,9 @@ class FilesystemLoader extends Loader */ static protected function isAbsolutePath($file) { - if ($file[0] == '/' || $file[0] == '\\' - || (strlen($file) > 3 && ctype_alpha($file[0]) - && $file[1] == ':' + if ($file[0] == '/' || $file[0] == '\\' + || (strlen($file) > 3 && ctype_alpha($file[0]) + && $file[1] == ':' && ($file[2] == '\\' || $file[2] == '/') ) ) {