8000 [Form] Optimize rendering · symfony/symfony@41e07c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41e07c9

Browse files
committed
[Form] Optimize rendering
1 parent ee5d975 commit 41e07c9

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ class FormExtension extends \Twig_Extension
3030
protected $themes;
3131
protected $varStack;
3232
protected $template;
33-
protected $rendering;
3433

3534
public function __construct(array $resources = array())
3635
{
3736
$this->themes = new \SplObjectStorage();
3837
$this->varStack = array();
3938
$this->blocks = new \SplObjectStorage();
40-
$this->rendering = array();
41-
4239
$this->resources = $resources;
4340
}
4441

@@ -133,7 +130,7 @@ public function renderRow(FormView $view, array $variables = array())
133130
),
134131
$variables
135132
);
136-
133+
137134
return $this->render($view, 'row', $variables);
138135
}
139136

@@ -235,37 +232,42 @@ protected function render(FormView $view, $section, array $variables = array())
235232
}
236233
}
237234

238-
$blocks = $this->getBlocks($view);
239235
$custom = '_'.$view->get('proto_id', $view->get('id'));
240-
$types = $view->get('types');
241-
$types[] = $custom;
242236
$rendering = $custom.$section;
237+
$blocks = $this->getBlocks($view);
243238

244-
$i = isset($this->rendering[$rendering]) ? $this->rendering[$rendering] - 1 : count ($types) - 1;
239+
if (isset($this->varStack[$rendering])) {
240+
$typeIndex = $this->varStack[$rendering]['typeIndex'] - 1;
241+
$types = $this->varStack[$rendering]['types'];
242+
$this->varStack[$rendering]['variables'] = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
243+
} else {
244+
$types = $view->get('types');
245+
$types[] = $custom;
246+
$typeIndex = count($types) - 1;
247+
$this->varStack[$rendering] = array (
248+
'variables' => array_replace_recursive($view->all(), $variables),
249+
'types' => $types,
250+
);
251+
}
245252

246253
do {
247-
$block = $types[$i].'_'.$section;
254+
$block = $types[$typeIndex].'_'.$section;
248255

249256
if (isset($blocks[$block])) {
250257

251-
$this->rendering[$rendering] = $i;
252-
253-
$this->varStack[$rendering] = array_replace_recursive(
254-
isset($this->varStack[$rendering]) ? $this->varStack[$rendering] : $view->all(),
255-
$variables
256-
);
258+
$this->varStack[$rendering]['typeIndex'] = $typeIndex;
257259

258-
$html = $this->template->renderBlock($block, $this->varStack[$rendering], $blocks);
260+
$html = $this->template->renderBlock($block, $this->varStack[$rendering]['variables'], $blocks);
259261

260262
if ($mainTemplate) {
261263
$view->setRendered();
262264
}
263265

264-
unset($this->varStack[$rendering], $this->rendering[$rendering]);
266+
unset($this->varStack[$rendering]);
265267

266268
return $html;
267269
}
268-
} while (--$i >= 0);
270+
} while (--$typeIndex >= 0);
269271

270272
throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $types)));
271273
}

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ class FormHelper extends Helper
3131

3232
protected $varStack;
3333

34-
protected $rendering;
35-
3634
protected $context;
3735

3836
public function __construct(EngineInterface $engine)
3937
{
4038
$this->engine = $engine;
4139
$this->varStack = array();
42-
$this->rendering = array();
4340
$this->context = array();
4441
}
4542

@@ -110,7 +107,7 @@ public function row(FormView $view, array $variables = array())
110107
),
111108
$variables
112109
);
113-
110+
114111
return $this->renderSection($view, 'row', $variables);
115112
}
116113

@@ -185,39 +182,44 @@ protected function renderSection(FormView $view, $section, array $variables = ar
185182
$template = null;
186183

187184
$custom = '_'.$view->get('proto_id', $view->get('id'));
188-
$types = $view->get('types');
189-
$types[] = $custom;
190185
$rendering = $custom.$section;
191186

192-
$i = isset($this->rendering[$rendering]) ? $this->rendering[$rendering] - 1 : count ($types) - 1;
187+
if (isset($this->varStack[$rendering])) {
188+
$typeIndex = $this->varStack[$rendering]['typeIndex'] - 1;
189+
$types = $this->varStack[$rendering]['types'];
190+
$this->varStack[$rendering]['variables'] = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
191+
} else {
192+
$types = $view->get('types');
193+
$types[] = $custom;
194+
$typeIndex = count($types) - 1;
195+
$this->varStack[$rendering] = array (
196+
'variables' => array_replace_recursive($view->all(), $variables),
197+
'types' => $types,
198+
);
199+
}
193200

194201
do {
195-
$block = $types[$i].'_'.$section;
202+
$block = $types[$typeIndex].'_'.$section;
196203
$template = $this->lookupTemplate($block);
197204

198205
if ($template) {
199206

200-
$this->rendering[$rendering] = $i;
201-
202-
$this->varStack[$rendering] = array_replace_recursive(
203-
isset($this->varStack[$rendering]) ? $this->varStack[$rendering] : $view->all(),
204-
$variables
205-
);
207+
B81E $this->varStack[$rendering]['typeIndex'] = $typeIndex;
206208

207-
$this->context[] = $this->varStack[$rendering];
209+
$this->context[] = $this->varStack[$rendering]['variables'];
208210

209-
$html = $this->engine->render($template, $this->varStack[$rendering]);
211+
$html = $this->engine->render($template, $this->varStack[$rendering]['variables']);
210212

211213
array_pop($this->context);
212-
unset($this->varStack[$rendering], $this->rendering[$rendering]);
214+
unset($this->varStack[$rendering]);
213215

214216
if ($mainTemplate) {
215217
$view->setRendered();
216218
}
217219

218220
return $html;
219221
}
220-
} while (--$i >= 0);
222+
} while (--$typeIndex >= 0);
221223

222224
throw new FormException(sprintf('Unable to render the form as none of the following blocks exist: "%s".', implode('", "', $types)));
223225
}

0 commit comments

Comments
 (0)
0