[Form] Fixed rendering using the PHP Templating engine

This commit is contained in:
Bernhard Schussek 2011-03-26 18:38:03 +01:00
parent fae319e77a
commit ed69d7fa35
10 changed files with 35 additions and 21 deletions

View File

@ -37,35 +37,42 @@ 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)) {
foreach ($blocks as &$block) {
$block = $block.'_'.$section;
if ($template = $this->lookupTemplate($block)) {
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));
}
}
throw new FormException(sprintf('The form theme is missing the "%s" template files', implode('", "', $blocks)));
}
protected function lookupTemplate($templateName)
{
if (isset(self::$cache[$templateName])) {
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;
}

View File

@ -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);
}
}

View File

@ -16,7 +16,8 @@
<parameter key="form.theme.factory.twig.class">Symfony\Component\Form\Renderer\Theme\TwigThemeFactory</parameter>
<parameter key="form.theme.factory.php.class">Symfony\Component\Form\Renderer\Theme\PhpThemeFactory</parameter>
<parameter key="form.theme.factory.phpengine.class">Symfony\Bundle\FrameworkBundle\Form\PhpEngineThemeFactory</parameter>
<parameter key="form.theme.template">TwigBundle::div_layout.html.twig</parameter>
<parameter key="form.theme.template.twig">TwigBundle::div_layout.html.twig</parameter>
<parameter key="form.theme.template.phpengine">FrameworkBundle:Form</parameter>
<parameter key="form.factory.class">Symfony\Component\Form\FormFactory</parameter>
<parameter key="form.csrf_protection.enabled">true</parameter>
<parameter key="form.csrf_protection.field_name">_token</parameter>
@ -82,7 +83,7 @@
<!-- Themes -->
<service id="form.theme.factory.twig" class="%form.theme.factory.twig.class%">
<argument type="service" id="twig" />
<argument>%form.theme.template%</argument>
<argument>%form.theme.template.twig%</argument>
</service>
<service id="form.theme.factory.default" class="%form.theme.factory.php.class%">
@ -91,6 +92,7 @@
<service id="form.theme.factory.php" class="%form.theme.factory.phpengine.class%">
<argument type="service" id="templating.engine.php" />
<argument>%form.theme.template.phpengine%</argument>
</service>
<!-- TemporaryStorage - where should we put this? -->
@ -129,8 +131,6 @@
<service id="form.type.field" class="Symfony\Component\Form\Type\FieldType">
<tag name="form.type" alias="field" />
<argument type="service" id="validator" />
<argument type="service" id="form.theme.factory" />
<argument>%form.theme.template%</argument>
</service>
<service id="form.type.form" class="Symfony\Component\Form\Type\FormType">
<tag name="form.type" alias="form" />

View File

@ -0,0 +1 @@
<?php if ($renderer->getVar('multipart')): ?>enctype="multipart/form-data"<?php endif ?>