diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index b949f534f493e..8d44288fb9e2c 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1053,6 +1053,21 @@ $registry->addType($registry->resolveType(new MyFormType())); ``` + * The method `renderBlock()` of the helper for the PHP Templating component was + deprecated and will be removed in Symfony 2.3. You should use `block()` instead. + + Before: + + ``` + renderBlock('widget_attributes') ?> + ``` + + After: + + ``` + block('widget_attributes') ?> + ``` + ### Validator * The methods `setMessage()`, `getMessageTemplate()` and diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index f77965f689f24..f07a8a486b7c3 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -12,11 +12,9 @@ namespace Symfony\Bridge\Twig\Extension; use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser; -use Symfony\Component\Form\FormView; -use Symfony\Component\Form\Exception\FormException; +use Symfony\Bridge\Twig\Form\TwigRendererInterface; +use Symfony\Component\Form\FormViewInterface; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; -use Symfony\Component\Form\Extension\Core\View\ChoiceView; -use Symfony\Component\Form\Util\FormUtil; /** * FormExtension extends Twig with form capabilities. @@ -26,21 +24,17 @@ */ class FormExtension extends \Twig_Extension { - protected $csrfProvider; - protected $resources; - protected $blocks; - protected $environment; - protected $themes; - protected $varStack; - protected $template; + /** + * This property is public so that it can be accessed directly from compiled + * templates without having to call a getter, which slightly decreases performance. + * + * @var \Symfony\Component\Form\FormRendererInterface + */ + public $renderer; - public function __construct(CsrfProviderInterface $csrfProvider = null, array $resources = array()) + public function __construct(TwigRendererInterface $renderer) { - $this->csrfProvider = $csrfProvider; - $this->themes = new \SplObjectStorage(); - $this->varStack = array(); - $this->blocks = new \SplObjectStorage(); - $this->resources = $resources; + $this->renderer = $renderer; } /** @@ -48,25 +42,11 @@ public function __construct(CsrfProviderInterface $csrfProvider = null, array $r */ public function initRuntime(\Twig_Environment $environment) { - $this->environment = $environment; - } - - /** - * Sets a theme for a given view. - * - * @param FormView $view A FormView instance - * @param array|string $resources An array of resource names|a resource name - */ - public function setTheme(FormView $view, $resources) - { - $this->themes->attach($view, (array) $resources); - $this->blocks = new \SplObjectStorage(); + $this->renderer->setEnvironment($environment); } /** - * Returns the token parser instance to add to the existing list. - * - * @return array An array of Twig_TokenParser instances + * {@inheritdoc} */ public function getTokenParsers() { @@ -76,305 +56,39 @@ public function getTokenParsers() ); } + /** + * {@inheritdoc} + */ public function getFunctions() { return array( - 'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))), - 'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))), - 'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))), - 'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))), - 'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))), - 'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))), - 'csrf_token' => new \Twig_Function_Method($this, 'getCsrfToken'), - '_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))), - '_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))), + 'form_enctype' => new \Twig_Function_Method($this, 'renderer->renderEnctype', array('is_safe' => array('html'))), + 'form_widget' => new \Twig_Function_Method($this, 'renderer->renderWidget', array('is_safe' => array('html'))), + 'form_errors' => new \Twig_Function_Method($this, 'renderer->renderErrors', array('is_safe' => array('html'))), + 'form_label' => new \Twig_Function_Method($this, 'renderer->renderLabel', array('is_safe' => array('html'))), + 'form_row' => new \Twig_Function_Method($this, 'renderer->renderRow', array('is_safe' => array('html'))), + 'form_rest' => new \Twig_Function_Method($this, 'renderer->renderRest', array('is_safe' => array('html'))), + 'csrf_token' => new \Twig_Function_Method($this, 'renderer->renderCsrfToken'), + '_form_is_choice_group' => new \Twig_Function_Method($this, 'renderer->isChoiceGroup', array('is_safe' => array('html'))), + '_form_is_choice_selected' => new \Twig_Function_Method($this, 'renderer->isChoiceSelected', array('is_safe' => array('html'))), ); } + /** + * {@inheritdoc} + */ public function getFilters() { return array( - 'humanize' => new \Twig_Filter_Function(__NAMESPACE__.'\humanize'), + 'humanize' => new \Twig_Filter_Method($this, 'renderer->humanize'), ); } - public function isChoiceGroup($label) - { - return FormUtil::isChoiceGroup($label); - } - - public function isChoiceSelected(FormView $view, ChoiceView $choice) - { - return FormUtil::isChoiceSelected($choice->getValue(), $view->getVar('value')); - } - - /** - * Renders the HTML enctype in the form tag, if necessary - * - * Example usage in Twig templates: - * - *