Example #1
0
 /**
  * {@inheritdoc}
  */
 public function renderBlock(FormView $view, $blockName, array $variables = array())
 {
     if (0 == count($this->variableStack)) {
         throw new FormException('This method should only be called while rendering a form element.');
     }
     $viewCacheKey = $view->vars[self::CACHE_KEY_VAR];
     $scopeVariables = end($this->variableStack[$viewCacheKey]);
     $resource = $this->engine->getResourceForBlockName($view, $blockName);
     if (!$resource) {
         throw new FormException(sprintf('No block "%s" found while rendering the form.', $blockName));
     }
     // Merge the passed with the existing attributes
     if (isset($variables['attr']) && isset($scopeVariables['attr'])) {
         $variables['attr'] = array_replace($scopeVariables['attr'], $variables['attr']);
     }
     // Merge the passed with the exist *label* attributes
     if (isset($variables['label_attr']) && isset($scopeVariables['label_attr'])) {
         $variables['label_attr'] = array_replace($scopeVariables['label_attr'], $variables['label_attr']);
     }
     // Do not use array_replace_recursive(), otherwise array variables
     // cannot be overwritten
     $variables = array_replace($scopeVariables, $variables);
     $this->variableStack[$viewCacheKey][] = $variables;
     // Do the rendering
     $html = $this->engine->renderBlock($view, $resource, $blockName, $variables);
     // Clear the stack
     array_pop($this->variableStack[$viewCacheKey]);
     return $html;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function renderBlock(FormView $view, $blockName, array $variables = array())
 {
     $resource = $this->engine->getResourceForBlockName($view, $blockName);
     if (!$resource) {
         throw new LogicException(sprintf('No block "%s" found while rendering the form.', $blockName));
     }
     $viewCacheKey = $view->vars[self::CACHE_KEY_VAR];
     // 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;
         $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
     if (isset($variables['attr']) && isset($scopeVariables['attr'])) {
         $variables['attr'] = array_replace($scopeVariables['attr'], $variables['attr']);
     }
     // Merge the passed with the exist *label* attributes
     if (isset($variables['label_attr']) && isset($scopeVariables['label_attr'])) {
         $variables['label_attr'] = array_replace($scopeVariables['label_attr'], $variables['label_attr']);
     }
     // Do not use array_replace_recursive(), otherwise array variables
     // cannot be overwritten
     $variables = array_replace($scopeVariables, $variables);
     $this->variableStack[$viewCacheKey][] = $variables;
     // Do the rendering
     $html = $this->engine->renderBlock($view, $resource, $blockName, $variables);
     // Clear the stack
     array_pop($this->variableStack[$viewCacheKey]);
     if ($varInit) {
         unset($this->variableStack[$viewCacheKey]);
     }
     return $html;
 }