Skip to content

Commit

Permalink
[Form] Improved FormRenderer::renderBlock() to be usable outside of f…
Browse files Browse the repository at this point in the history
…orm blocks
  • Loading branch information
webmozart committed Apr 18, 2013
1 parent e51c560 commit 0ea75db
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Symfony/Component/Form/FormRenderer.php
Expand Up @@ -87,17 +87,29 @@ public function renderCsrfToken($intention)
*/
public function renderBlock(FormView $view, $blockName, array $variables = array())
{
if (0 == count($this->variableStack)) {
throw new Exception('This method should only be called while rendering a form element.');
$resource = $this->engine->getResourceForBlockName($view, $blockName);

if (!$resource) {
throw new Exception(sprintf('No block "%s" found while rendering the form.', $blockName));
}

$viewCacheKey = $view->vars[self::CACHE_KEY_VAR];
$scopeVariables = end($this->variableStack[$viewCacheKey]);

$resource = $this->engine->getResourceForBlockName($view, $blockName);
// The variables are cached globally for a view (instead of for the
// current suffix)
if (!isset($this->variableStack[$viewCacheKey])) {
$this->variableStack[$viewCacheKey] = array();

if (!$resource) {
throw new Exception(sprintf('No block "%s" found while rendering the form.', $blockName));
// The default variable scope contains all view variables, merged with
// the variables passed explicitly to the helper
$scopeVariables = $view->vars;

$varInit = true;
} else {
// Reuse the current scope and merge it with the explicitly passed variables
$scopeVariables = end($this->variableStack[$viewCacheKey]);

$varInit = false;
}

// Merge the passed with the existing attributes
Expand All @@ -122,6 +134,10 @@ public function renderBlock(FormView $view, $blockName, array $variables = array
// Clear the stack
array_pop($this->variableStack[$viewCacheKey]);

if ($varInit) {
unset($this->variableStack[$viewCacheKey]);
}

return $html;
}

Expand Down Expand Up @@ -191,6 +207,8 @@ public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $va
// The variables are cached globally for a view (instead of for the
// current suffix)
if (!isset($this->variableStack[$viewCacheKey])) {
$this->variableStack[$viewCacheKey] = array();

// The default variable scope contains all view variables, merged with
// the variables passed explicitly to the helper
$scopeVariables = $view->vars;
Expand Down

0 comments on commit 0ea75db

Please sign in to comment.