Development

Changeset 6455

You must first sign up to be able to contribute.

Changeset 6455

Show
Ignore:
Timestamp:
12/11/07 11:45:26 (1 year ago)
Author:
dwhittle
Message:

dwhittle: added ability to set view attributes from view.yml files + ability to pass parameters via setComponentSlot()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/dwhittle/lib/config/sfViewConfigHandler.class.php

    r6259 r6455  
    8282      $data[] = $this->addHtmlHead($viewName); 
    8383      $data[] = $this->addEscaping($viewName); 
    84  
     84      $data[] = $this->addAttributes($viewName); 
    8585      $data[] = $this->addHtmlAsset($viewName); 
    8686 
     
    9797    $data[] = $this->addHtmlHead(); 
    9898    $data[] = $this->addEscaping(); 
    99  
     99    $data[] = $this->addAttributes(); 
    100100    $data[] = $this->addHtmlAsset(); 
    101101    $data[] = ($first ? '' : "}")."\n"; 
     
    147147 
    148148    $components = $this->mergeConfigValue('components', $viewName); 
     149 
    149150    foreach ($components as $name => $component) 
    150151    { 
    151       if (!is_array($component) || count($component) < 1) 
    152       { 
    153         $component = array(null, null); 
    154       } 
    155  
    156       $data .= "  \$this->setComponentSlot('{$name}', '{$component[0]}', '{$component[1]}');\n"; 
     152      if(!is_array($component) || count($component) < 1) 
     153      { 
     154        $component = array(null, null, null); // set component empty 
     155      } 
     156 
     157      $attributes = isset($component[2]) && is_array($component[2]) ? array_values($component[2]) : array(); 
     158      if(!empty($attributes)) 
     159      { 
     160        $cnt = count($attributes); 
     161        for($i = 0; $i < $cnt; $i++) // normalize yaml parsing of nested array 
     162        { 
     163         foreach ($attributes[$i] as $key => $value) 
     164         { 
     165          $attributes[$key] = $value; 
     166          unset($attributes[$i]); 
     167         } 
     168        } 
     169      } 
     170      $attributes = var_export($attributes, true); 
     171 
     172      $data .= "  \$this->setComponentSlot('{$name}', '{$component[0]}', '{$component[1]}', {$attributes});\n"; 
    157173      $data .= "  if (sfConfig::get('sf_logging_enabled')) { \$this->context->getEventDispatcher()->notify(new sfEvent(\$this, 'application.log', array(sprintf('Set component \"%s\" (%s/%s)', '{$name}', '{$component[0]}', '{$component[1]}')))); }\n"; 
    158174    } 
     
    366382    return implode("\n", $data)."\n"; 
    367383  } 
     384 
     385  /** 
     386   * Adds attributes to the view. 
     387   * 
     388   * @param array The attributes to set 
     389   * 
     390   * @return string The PHP statement 
     391   */ 
     392  protected function addAttributes($viewName = '') 
     393  { 
     394    $data = array(); 
     395 
     396    $attributes = $this->getConfigValue('attributes', $viewName, array()); 
     397 
     398    if(is_array($attributes) && !empty($attributes)) 
     399    { 
     400      $data[] = sprintf("  \$this->getAttributeHolder()->add(%s);", var_export($attributes, true)); 
     401    } 
     402 
     403    return implode("\n", $data)."\n"; 
     404  } 
    368405} 
  • branches/dwhittle/lib/plugins/sfCompat10Plugin/lib/helper/PartialHelper.php

    r6269 r6455  
    6565  if ($componentSlot = $viewInstance->getComponentSlot($name)) 
    6666  { 
    67     return get_component($componentSlot[0], $componentSlot[1], $vars); 
     67    if (!empty($componentSlot[0])) // ignore component slots that are present, but empty 
     68    { 
     69      return get_component($componentSlot[0], $componentSlot[1], array_merge($componentSlot[2], $vars)); 
     70    } 
     71 
     72    return null; 
    6873  } 
    6974} 
  • branches/dwhittle/lib/view/sfView.class.php

    r6180 r6455  
    401401 
    402402  /** 
    403    * Sets the module and action to be executed in place of a particular template attribute
    404    * 
    405    * @param string A template attribute name 
     403   * Sets the module and action to be executed for component slot
     404   * 
     405   * @param string The component slot name 
    406406   * @param string A module name 
    407407   * @param string A component name 
    408    */ 
    409   public function setComponentSlot($attributeName, $moduleName, $componentName) 
    410   { 
    411     $this->componentSlots[$attributeName]                   = array(); 
    412     $this->componentSlots[$attributeName]['module_name']    = $moduleName; 
    413     $this->componentSlots[$attributeName]['component_name'] = $componentName; 
     408   * @param array Parameters to pass to component 
     409   */ 
     410  public function setComponentSlot($name, $moduleName, $componentName, $parameters = array()) 
     411  { 
     412    $this->componentSlots[$name]                   = array(); 
     413    $this->componentSlots[$name]['module_name']    = $moduleName; 
     414    $this->componentSlots[$name]['component_name'] = $componentName; 
     415    $this->componentSlots[$name]['parameters']     = is_array($parameters) ? $parameters : array(); 
    414416  } 
    415417 
     
    435437  public function getComponentSlot($name) 
    436438  { 
    437     if (isset($this->componentSlots[$name]) && $this->componentSlots[$name]['module_name'] && $this->componentSlots[$name]['component_name']
    438     { 
    439       return array($this->componentSlots[$name]['module_name'], $this->componentSlots[$name]['component_name']); 
     439    if (isset($this->componentSlots[$name]) && isset($this->componentSlots[$name]['module_name']) && isset($this->componentSlots[$name]['component_name']) && isset($this->componentSlots[$name]['parameters'])
     440    { 
     441      return array($this->componentSlots[$name]['module_name'], $this->componentSlots[$name]['component_name'],  $this->componentSlots[$name]['parameters']); 
    440442    } 
    441443