Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 167 lines (132 sloc) 4.168 kb
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
1 <?php
2
1c9d761 @blue-eyes normalized license messages in PHP files
blue-eyes authored
3 /*
4 * This file is part of the Symfony package.
5 *
506e5b7 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
1c9d761 @blue-eyes normalized license messages in PHP files
blue-eyes authored
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
12 namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
13
14 use Symfony\Component\Templating\Helper\Helper;
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
15 use Symfony\Component\Templating\EngineInterface;
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
16 use Symfony\Component\Form\FormView;
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
17 use Symfony\Component\Form\Exception\FormException;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
18
19 /**
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
20 *
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
21 *
506e5b7 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
22 * @author Fabien Potencier <fabien@symfony.com>
23 * @author Bernhard Schussek <bernhard.schussek@symfony.com>
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
24 */
25 class FormHelper extends Helper
26 {
27 static protected $cache = array();
28
29 protected $engine;
30
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
31 protected $varStack = array();
32
1b82ece @stfalcon fix bug in Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper
stfalcon authored
33 public function __construct(EngineInterface $engine)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
34 {
35 $this->engine = $engine;
36 }
37
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
38 public function attributes()
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
39 {
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
40 $html = '';
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
41 $attr = array();
42
43 if (count($this->varStack) > 0) {
44 $vars = end($this->varStack);
45
46 if (isset($vars['attr'])) {
47 $attr = $vars['attr'];
48 }
49
50 if (isset($vars['id'])) {
51 $attr['id'] = $vars['id'];
52 }
53 }
54
55 foreach ($attr as $k => $v) {
56 $html .= ' '.$this->engine->escape($k).'="'.$this->engine->escape($v).'"';
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
57 }
58
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
59 return $html;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
60 }
61
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
62 public function enctype(FormView $view)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
63 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
64 return $this->renderSection($view, 'enctype');
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
65 }
66
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
67 public function widget(FormView $view, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
68 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
69 return trim($this->renderSection($view, 'widget', $variables));
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
70 }
71
72 /**
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
73 * Renders the entire form field "row".
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
74 *
c4c66ff @pborreli [Phpdoc] Cleaning/fixing
pborreli authored
75 * @param FormView $view
76 * @param array $variables
77 *
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
78 * @return string
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
79 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
80 public function row(FormView $view, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
81 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
82 return $this->renderSection($view, 'row', $variables);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
83 }
84
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
85 public function label(FormView $view, $label = null)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
86 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
87 return $this->renderSection($view, 'label', null === $label ? array() : array('label' => $label));
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
88 }
89
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
90 public function errors(FormView $view)
c43d690 @weaverryan [Form] Adding a row() PHP helper equivalent to the Twig form_row() fo…
weaverryan authored
91 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
92 return $this->renderSection($view, 'errors');
c43d690 @weaverryan [Form] Adding a row() PHP helper equivalent to the Twig form_row() fo…
weaverryan authored
93 }
94
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
95 public function rest(FormView $view, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
96 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
97 return $this->renderSection($view, 'rest', $variables);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
98 }
99
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
100 protected function renderSection(FormView $view, $section, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
101 {
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
102 $template = null;
ef0c270 @henrikbjorn [Form] change FormView::setVar,getVar,getVars,hasVar to set,get,all,has
henrikbjorn authored
103 $blocks = $view->get('types');
a259d44 @webmozart [Form] Added tests for blocks/templates in the format _<ID>_(widget|r…
webmozart authored
104 array_unshift($blocks, '_'.$view->get('id'));
105
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
106 foreach ($blocks as &$block) {
107 $block = $block.'_'.$section;
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
108 $template = $this->lookupTemplate($block);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
109
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
110 if ($template) {
111 break;
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
112 }
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
113 }
114
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
115 if (!$template) {
116 throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks)));
117 }
118
119 if ('widget' === $section || 'row' === $section) {
850e23b @udat [Templating] removed unused argument passed to setRendered() method
udat authored
120 $view->setRendered();
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
121 }
122
ef0c270 @henrikbjorn [Form] change FormView::setVar,getVar,getVars,hasVar to set,get,all,has
henrikbjorn authored
123 return $this->render($template, array_merge($view->all(), $variables));
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
124 }
125
126 public function render($template, array $variables = array())
127 {
128 array_push($this->varStack, array_merge(
129 count($this->varStack) > 0 ? end($this->varStack) : array(),
130 $variables
131 ));
132
133 $html = $this->engine->render($template, end($this->varStack));
134
135 array_pop($this->varStack);
136
137 return $html;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
138 }
139
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
140 protected function lookupTemplate($templateName)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
141 {
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
142 if (isset(self::$cache[$templateName])) {
143 return self::$cache[$templateName];
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
144 }
145
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
146 $template = $templateName.'.html.php';
147 /*
148 if ($this->templateDir) {
149 $template = $this->templateDir.':'.$template;
150 }
151 */
152 $template = 'FrameworkBundle:Form:'.$template;
153 if (!$this->engine->exists($template)) {
154 $template = false;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
155 }
156
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
157 self::$cache[$templateName] = $template;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
158
159 return $template;
160 }
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
161
162 public function getName()
163 {
164 return 'form';
165 }
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
166 }
Something went wrong with that request. Please try again.