Show
Ignore:
Timestamp:
08/13/05 10:45:45 (3 years ago)
Author:
schst
Message:

Parser now uses the element's id for the placeholder. Parser_patTemplateRenderer and Parser_SimpleRenderer now use the matching renderers as delegates (fixes bug #184 and #185). This commit may break some scripts, but the parser was labelled alpha before.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/patForms/Parser/SimpleRenderer.php

    r317 r320  
    2828 * @license     LGPL 
    2929 * @copyright   PHP Application Tools <http://www.php-tools.net> 
    30  * @version     1.0 
    3130 */ 
    3231class patForms_Parser_SimpleRenderer extends patForms_Parser 
     
    4241    function render(&$patForms, $args = null) 
    4342    { 
    44         $serializedElements = array(); 
    45          
    46         $cnt = count($patForms->elements); 
    47         for ($i=0; $i < $cnt; $i++) { 
    48             // first, serialize the element as this also initializes the attribute collection. 
    49             $serialized = $patForms->elements[$i]->serialize(); 
    50             if ($serialized === false) { 
    51                 patErrorManager::raiseWarning( 
    52                     PATFORMS_PARSER_ERROR_ELEMENT_NOT_SERIALIZEABLE,  
    53                     "Element '".get_class($patForms->elements[$i])."' could not return serialized data."  
    54                 ); 
    55                 continue; 
    56             } 
    57             $serializedElements[($patForms->elements[$i]->getName())] = $serialized; 
    58         } 
    59         $html = $this->getHTML(); 
    60         foreach ($serializedElements as $name => $element) { 
    61             $varName = sprintf("{PATFORMS_ELEMENT_%s}", strtoupper($name)); 
    62             $html    = str_replace($varName, $element, $html); 
    63         } 
    64  
    65         $name = $patForms->getName(); 
    66         $varName = sprintf("{PATFORMS_FORM_%s_START}", strtoupper($name)); 
    67         $html    = str_replace($varName, $patForms->serializeStart(), $html); 
    68          
    69         $varName = sprintf("{PATFORMS_FORM_%s_END}", strtoupper($name)); 
    70         $html    = str_replace($varName, $patForms->serializeEnd(), $html); 
    71         return  $html; 
     43        $renderer = &patForms::createRenderer('String'); 
     44        $renderer->setTemplate($this->getHTML()); 
     45        $renderer->setPlaceholder($this->_placeholder, 'id'); 
     46        $renderer->setFormPlaceholders($this->_placeholder_form_start, $this->_placeholder_form_end); 
     47         
     48        return $renderer->render($patForms, $args); 
    7249    } 
    7350} 
  • trunk/patForms/Parser/patTemplateRenderer.php

    r110 r320  
    5050    * @param    object  patTemplate object 
    5151    */ 
    52     function setTemplate( &$tmpl
     52    function setTemplate(&$tmpl
    5353    { 
    54         $this->_tmpl   =   &$tmpl; 
     54        $this->_tmpl = &$tmpl; 
    5555    } 
    5656 
     
    6363    * @return   string  $html               HTML code 
    6464    */ 
    65     function render( &$patForms, $args = null
     65    function render(&$patForms, $args = null
    6666    { 
    67         $serializedElements =   array(); 
    68         $elementAttribs     =   array(); 
    69          
    70         $cnt    =   count( $patForms->elements ); 
    71         for( $i=0; $i < $cnt; $i++ ) 
    72         { 
    73             // first, serialize the element as this also initializes the attribute collection. 
    74             $serialized =   $patForms->elements[$i]->serialize(); 
    75             if( $serialized === false ) 
    76             { 
    77                 patErrorManager::raiseWarning( 
    78                     PATFORMS_PARSER_ERROR_ELEMENT_NOT_SERIALIZEABLE,  
    79                     'Element \''.get_class( $patForms->elements[$i] ).'\' could not return serialized data.'  
    80                 ); 
    81                 continue; 
    82             } 
    83             $elName                         =   $patForms->elements[$i]->getName(); 
    84             $serializedElements[$elName]    =   $serialized; 
    85             $elementAttribs[$elName]        =   $patForms->elements[$i]->getAttributes(); 
    86         } 
    87          
    88         // no template has been specified => use the default 
    89         if( $this->_tmpl == null ) 
    90         { 
    91             $this->_tmpl    =   &patForms_Parser::getStaticProperty( 'tmpl' ); 
     67        if ($this->_tmpl == null) { 
     68            $this->_tmpl = &patForms_Parser::getStaticProperty('tmpl'); 
    9269        } 
    9370 
     71        $renderer = &patForms::createRenderer('patTemplate'); 
     72        $renderer = &new patForms_Renderer_patTemplate(); 
     73        $renderer->setTemplate($this->_tmpl); 
     74         
     75        $renderer->setPlaceholder($this->_placeholder); 
     76        $renderer->setFormPlaceholders($this->_placeholder_form_start, $this->_placeholder_form_end); 
     77         
    9478        // check, whether the file has been loaded 
    95         if( !$this->_tmpl->exists( $args['template'] ) ) 
    96         { 
    97             $this->_tmpl->readTemplatesFromFile( $this->_outputFile ); 
     79        if (!$this->_tmpl->exists($args['template'])) { 
     80            $this->_tmpl->readTemplatesFromFile($this->_outputFile); 
    9881        } 
    9982 
    100         foreach( $serializedElements as $name => $element ) 
    101         { 
    102             $this->_tmpl->addVar( $args['template'], sprintf( $this->_placeholder, $name ), $element ); 
    103  
    104             // copy the attribute collection 
    105             $tmplVars = $elementAttribs[$name]; 
    106              
    107             // remove any arrays in the variables to add, as that could  
    108             // lead to problems with the patTemplate output. 
    109             foreach( $tmplVars as $key => $val ) 
    110                 if( is_array( $val ) ) 
    111                     $tmplVars[$key] = ''; 
    112              
    113             // add the attribute collection as vars to the template 
    114             $this->_tmpl->addVars( $args['template'], $tmplVars, $name.'_' ); 
    115         } 
    116  
    117         $this->_tmpl->addVar( $args['template'], sprintf( $this->_placeholder_form_start, $patForms->getName() ), $patForms->serializeStart() ); 
    118         $this->_tmpl->addVar( $args['template'], sprintf( $this->_placeholder_form_end, $patForms->getName() ), $patForms->serializeEnd() ); 
    119  
    120  
    121         if( !isset( $args['errorTemplate'] ) ) 
    122         { 
    123             return  true; 
    124         } 
    125          
    126         return  $this->_renderErrors( $patForms, $args ); 
     83        return $renderer->render($patForms, $args); 
    12784    } 
    12885 
    129    /** 
    130     * render the errors 
    131     * 
    132     * @access   private 
    133     * @todo     check for special '__form' element 
    134     */ 
    135     function _renderErrors( &$patForms, $args = null ) 
    136     { 
    137         if( isset( $this->errorsRendered[$patForms->getName()] ) ) 
    138         { 
    139             return true; 
    140         } 
    141      
    142         /** 
    143          * render the errors 
    144          */ 
    145         if( $patForms->isSubmitted() && !$patForms->validateForm() ) 
    146         { 
    147             if( isset( $args['errorTemplateContainer'] ) ) 
    148             { 
    149                 $this->_tmpl->setAttribute( $args['errorTemplateContainer'], 'visibility', 'visible' ); 
    150             } 
    151             $validationErrors   =   $patForms->getValidationErrors(); 
    152  
    153             foreach( $validationErrors as $fieldName => $errors ) 
    154             { 
    155                 if( empty( $errors ) ) 
    156                 { 
    157                     continue; 
    158                 } 
    159                  
    160                 $field =& $patForms->getElement( $fieldName ); 
    161  
    162                 // workaround for patTemplate Bug! - an array in the 
    163                 // added attribute collection could lead to the 
    164                 // template to be repeated too many times. 
    165                 $atts   =   $field->getAttributes(); 
    166                 foreach( $atts as $key => $value ) 
    167                 { 
    168                     if( is_array( $atts[$key] ) ) 
    169                     { 
    170                         unset( $atts[$key] ); 
    171                     } 
    172                 } 
    173                  
    174                 $this->_tmpl->addVars( $args['errorTemplate'], $atts, 'FIELD_' ); 
    175                  
    176                 foreach( $errors as $error ) 
    177                 { 
    178                     $error['field'] = $fieldName; 
    179                     $this->_tmpl->addVars( $args['errorTemplate'], $error, 'ERROR_' ); 
    180                     $this->_tmpl->parseTemplate( $args['errorTemplate'], 'a' ); 
    181                 } 
    182             } 
    183         } 
    184          
    185         $this->errorsRendered[$patForms->getName()] = true; 
    186          
    187         return  true; 
    188     } 
    189      
    19086   /** 
    19187    * get the placeholder for an element