Subversion Repositories phpLibraryV2

Rev

Rev 10 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10 Rev 47
Line 57... Line 57...
57
                    'varname' => '#<datafield +([^>]+)>#si',
57
                    'varname' => '#<datafield +([^>]+)>#si',
58
                    'session' => '#<session +([^>]+)>#si'
58
                    'session' => '#<session +([^>]+)>#si'
59
                 );
59
                 );
60
   protected static $delimiters = array( 'open' => '<', 'close' => '>' );
60
   protected static $delimiters = array( 'open' => '<', 'close' => '>' );
61
   protected $filename; // holds the name of the template file
61
   protected $filename; // holds the name of the template file
-
 
62
   
-
 
63
   // this is a key inside the data hash that holds the value. If empty, the value is treated as the value
-
 
64
   // for example, if $hashKey is set to 'value', data used in processTemplate is assumed to be $data[$key]['value']
-
 
65
   // instead of just $data[$key]
-
 
66
   protected $hashKey = '';
62
 
67
 
63
   public static $templatePath; // path to search for templates
68
   public static $templatePath; // path to search for templates
64
   public $template; // holds entire template for processing
69
   public $template; // holds entire template for processing
65
   
70
   
66
   /*
71
   /*
Line 71... Line 76...
71
    * description: If filename is given, will prepend $templatePath to it and attempt to load
76
    * description: If filename is given, will prepend $templatePath to it and attempt to load
72
    *              from disk.
77
    *              from disk.
73
    *              if $templatePath is defined, will set the path FOR ALL INSTANCES. 
78
    *              if $templatePath is defined, will set the path FOR ALL INSTANCES. 
74
    */
79
    */
75
 
80
 
76
   public function __construct ( $filename = null, $templatePath = null ) {
81
   public function __construct ( $filename = null, $templatePath = null, $hashKey = '' ) {
77
      if ( isset( $templatePath ) ) {
82
      if ( isset( $templatePath ) ) {
78
         self::$templatePath = $templatePath;
83
         self::$templatePath = $templatePath;
79
      } // if
84
      } // if
80
      if ( isset( $filename ) ) {
85
      if ( isset( $filename ) ) {
81
         $this->FileName( $filename );
86
         $this->FileName( $filename );
82
         $this->loadTemplate();
87
         $this->loadTemplate();
83
      } // if
88
      } // if
-
 
89
      $this->hashKey = $hashKey;
84
   } // construct
90
   } // construct
85
   
91
   
86
   /*
92
   /*
87
    * name:        FileName
93
    * name:        FileName
88
    * parameters:  $filename -- optional override of filename stored in instance
94
    * parameters:  $filename -- optional override of filename stored in instance
Line 218... Line 224...
218
    *             
224
    *             
219
    *             The processed template is returned by the routine.
225
    *             The processed template is returned by the routine.
220
    */
226
    */
221
                 
227
                 
222
   private function processTemplate ( $data, $template ) {
228
   private function processTemplate ( $data, $template ) {
-
 
229
 
-
 
230
      // if they used a hashKey (the value is stored in an element of $data), replace the hash with the value
-
 
231
      // so, $data[$key][$this->hashkey] becomes just $data[$key]
-
 
232
      //print '<pre>processTemplate::data' . print_r( $data, true ) . '</pre>';
-
 
233
      if ( $this->hashKey ) {
-
 
234
         $tempData = array();
-
 
235
         //print "<pre>Processing hashKey $this->hashKey\n</pre>";
-
 
236
         foreach ( $data as $key => $value ) {
-
 
237
            //print "<pre>Checking $key\n</pre>";
-
 
238
            if ( is_array ($value ) && isset( $value[$this->hashKey] ) ) {
-
 
239
               //print "<pre>   it matches\n</pre>";
-
 
240
               $tempData[$key] = $value[$this->hashKey];
-
 
241
            } else {
-
 
242
               $tempData[$key] = $value;
-
 
243
            }
-
 
244
         }
-
 
245
         $data = $tempData;
-
 
246
      }
-
 
247
      //print "<pre>processTemplate::data\n" . print_r( $data, true ) . '</pre>'; 
-
 
248
      //print "Template is <pre>$template</pre>"; 
-
 
249
      //die;
223
      // process loops first
250
      // process loops first
224
      while ( preg_match( self::$regexes['loop'], $template, $matches ) ) {
251
      while ( preg_match( self::$regexes['loop'], $template, $matches ) ) {
225
         $block = preg_quote($matches[0], '/'); // the full block that matches, so we can replace it
252
         $block = preg_quote($matches[0], '/'); // the full block that matches, so we can replace it
226
         $dataKey = $matches[1]; // this is the name of the key into $data
253
         $dataKey = $matches[1]; // this is the name of the key into $data
227
         $segment  = $matches[2]; // and this is the template segment to be processed
254
         $segment  = $matches[2]; // and this is the template segment to be processed
Line 245... Line 272...
245
      } // while
272
      } // while
246
      // next, any direct variable replacements
273
      // next, any direct variable replacements
247
      while ( preg_match( self::$regexes['varname'], $template, $matches ) ) {
274
      while ( preg_match( self::$regexes['varname'], $template, $matches ) ) {
248
         $block = preg_quote($matches[0], '/'); // the full block that matches, so we can replace it
275
         $block = preg_quote($matches[0], '/'); // the full block that matches, so we can replace it
249
         $varname = $matches[1]; // the variable whose value we want here
276
         $varname = $matches[1]; // the variable whose value we want here
-
 
277
         //print "<pre>$varname\n" . print_r($data[$varname], true) . "</pre>";
250
         // replace
278
         // replace
-
 
279
         
251
         $template = preg_replace( "/$block/", 
280
         $template = preg_replace( "/$block/", 
252
                                 isset( $data[$varname] ) ? $data[$varname] : '',
281
                                 isset( $data[$varname] ) ? $data[$varname] : '',
253
                                 $template );
282
                                 $template );
-
 
283
         
254
      } // while
284
      } // while
255
      // and finally, session variables
285
      // and finally, session variables
256
      while ( preg_match( self::$regexes['session'], $template, $matches ) ) {
286
      while ( preg_match( self::$regexes['session'], $template, $matches ) ) {
257
         $block = preg_quote($matches[0], '/'); // the full block that matches, so we can replace it
287
         $block = preg_quote($matches[0], '/'); // the full block that matches, so we can replace it
258
         $varname = $matches[1]; // the session variable name
288
         $varname = $matches[1]; // the session variable name