4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\Extension
;
14 use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser
;
15 use Symfony\Bridge\Twig\Form\TwigRendererInterface
;
16 use Symfony\Component\Form\Extension\Core\View\ChoiceView
;
19 * FormExtension extends Twig with form capabilities.
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Bernhard Schussek <bschussek@gmail.com>
24 class FormExtension
extends \Twig_Extension
27 * This property is public so that it can be accessed directly from compiled
28 * templates without having to call a getter, which slightly decreases performance.
30 * @var TwigRendererInterface
34 public function __construct(TwigRendererInterface
$renderer)
36 $this->renderer
= $renderer;
42 public function initRuntime(\Twig_Environment
$environment)
44 $this->renderer
->setEnvironment($environment);
50 public function getTokenParsers()
53 // {% form_theme form "SomeBundle::widgets.twig" %}
54 new FormThemeTokenParser(),
61 public function getFunctions()
64 'form_enctype' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\FormEnctypeNode', array('is_safe' => array('html'))),
65 'form_widget' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
66 'form_errors' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
67 'form_label' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
68 'form_row' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
69 'form_rest' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
70 'form' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
71 'form_start' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
72 'form_end' => new \
Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
73 'csrf_token' => new \
Twig_Function_Method($this, 'renderer->renderCsrfToken'),
80 public function getFilters()
83 'humanize' => new \
Twig_Filter_Method($this, 'renderer->humanize'),
90 public function getTests()
93 'selectedchoice' => new \
Twig_Test_Method($this, 'isSelectedChoice'),
98 * Returns whether a choice is selected for a given form value.
100 * Unfortunately Twig does not support an efficient way to execute the
101 * "is_selected" closure passed to the template by ChoiceType. It is faster
102 * to implement the logic here (around 65ms for a specific form).
104 * Directly implementing the logic here is also faster than doing so in
105 * ChoiceView (around 30ms).
107 * The worst option tested so far is to implement the logic in ChoiceView
108 * and access the ChoiceView method directly in the template. Doing so is
109 * around 220ms slower than doing the method call here in the filter. Twig
110 * seems to be much more efficient at executing filters than at executing
111 * methods of an object.
113 * @param ChoiceView $choice The choice to check.
114 * @param string|array $selectedValue The selected value to compare.
116 * @return Boolean Whether the choice is selected.
118 * @see ChoiceView::isSelected()
120 public function isSelectedChoice(ChoiceView
$choice, $selectedValue)
122 if (is_array($selectedValue)) {
123 return false !== array_search($choice->value
, $selectedValue, true);
126 return $choice->value
=== $selectedValue;
132 public function getName()