From 7e6bddedf90319a3e27c3a14b4f6753e38f96007 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 28 Oct 2010 09:50:00 +0200 Subject: [PATCH] [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 --- .../TwigBundle/Extension/FormExtension.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php index 2142c5cd2a5c..3aac830ab073 100644 --- a/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php +++ b/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php @@ -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) @@ -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, @@ -116,6 +118,10 @@ 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() )); @@ -123,6 +129,10 @@ public function renderHidden(FieldGroupInterface $form) public function renderErrors($formOrField) { + if (null === $this->templates) { + $this->templates = $this->resolveResources($this->resources); + } + return $this->templates['errors']->getBlock('errors', array( 'errors' => $formOrField->getErrors() )); @@ -130,6 +140,10 @@ public function renderErrors($formOrField) 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(),