Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[TwigBundle] moved Form extension initialization as late as possible
Because

 * it's better for performance (no need to init form templates if there is no forms)
 * right now, it crashes for all renderer except HTML (because the form templates obviously only exist for the HTML renderer)

The only other possible fix would be to force those resources to always use the HTML renderer
  • Loading branch information
fabpot committed Oct 28, 2010
1 parent 7f8c540 commit 7e6bdde
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php
Expand Up @@ -48,8 +48,6 @@ public function __construct(HtmlGeneratorInterface $generator, array $resources
public function initRuntime(\Twig_Environment $environment)
{
$this->environment = $environment;

$this->templates = $this->resolveResources($this->resources);
}

public function setTheme(FieldGroupInterface $group, array $resources)
Expand Down Expand Up @@ -94,6 +92,10 @@ public function renderEnctype(Form $form)

public function render(FieldInterface $field, array $attributes = array())
{
if (null === $this->templates) {
$this->templates = $this->resolveResources($this->resources);
}

if ($field instanceof Form || get_class($field) === 'Symfony\Component\Form\FieldGroup') {
return $this->templates['group']->getBlock('group', array(
'group' => $field,
Expand All @@ -116,20 +118,32 @@ public function render(FieldInterface $field, array $attributes = array())

public function renderHidden(FieldGroupInterface $form)
{
if (null === $this->templates) {
$this->templates = $this->resolveResources($this->resources);
}

return $this->templates['hidden']->getBlock('hidden', array(
'fields' => $form->getHiddenFields()
));
}

public function renderErrors($formOrField)
{
if (null === $this->templates) {
$this->templates = $this->resolveResources($this->resources);
}

return $this->templates['errors']->getBlock('errors', array(
'errors' => $formOrField->getErrors()
));
}

public function renderLabel(FieldInterface $field, $label = null, array $attributes = array())
{
if (null === $this->templates) {
$this->templates = $this->resolveResources($this->resources);
}

return $this->templates['label']->getBlock('label', array(
'id' => $field->getId(),
'key' => $field->getKey(),
Expand Down

0 comments on commit 7e6bdde

Please sign in to comment.