TemplateObject.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:4k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. class TemplateObject extends BaseObject {
  3.    var $registered_tags;
  4.    var $registered_files;
  5.    var $template_var_start;
  6.    var $template_var_end;
  7.    var $with_php;
  8.    var $return_contents;
  9.    Function TemplateObject() {
  10.       $this->BaseObject( 'TemplateObject' );
  11.       $this->registered_tags        = Array();
  12.       $this->registered_files       = Array();
  13.       $this->template_var_start     = '[-';
  14.       $this->template_var_end       = '-]';
  15.       $this->with_php               = 0;
  16.       $this->return_contents        = 0;
  17.    }
  18.    Function Register( $tag, $value ) {
  19.       if ( isset( $this->registered_files[ $tag ] ) ) {
  20.          return false;
  21.       } else {
  22.          $this->registered_tags[ $tag ] = $value;
  23.          return true;
  24.       }
  25.    }
  26.    Function RegisterFile( $tag, $value ) {
  27.       if ( isset( $this->registered_files[ $tag ] ) ) {
  28.          return false;
  29.       } else {
  30.          $this->registered_files[ $tag ] = $value;
  31.          return true;
  32.       }
  33.    }
  34.    Function UnRegister( $tag ) {
  35.       $t_array = Array();
  36.       $success = 0;
  37.       reset( $this->registered_tags );
  38.       while( list( $key, $value ) = each( $this->registered_tags ) ) {
  39.          if ( $key != $tag ) {
  40.             $t_array[ $key ] = $value;
  41.          } else {
  42.             $success = 1;
  43.          }
  44.       }
  45.       $this->registered_tags = $t_array;
  46.       return $success;
  47.    }
  48.    Function UnRegisterFile( $tag ) {
  49.       $t_array = Array();
  50.       $success = 0;
  51.       reset( $this->registered_files );
  52.       while( list( $key, $value ) = each( $this->registered_files ) ) {
  53.          if ( $key != $tag ) {
  54.             $t_array[ $key ] = $value;
  55.          } else {
  56.             $success = 1;
  57.          }
  58.       }
  59.       $this->registered_files = $t_array;
  60.       return $success;
  61.    }
  62.    Function GetValue( $tag ) {
  63.       if ( isset( $this->registered_tags[ $tag ] ) ) {
  64.       return $this->registered_tags[ $tag ];
  65.       }
  66.       if ( isset( $this->registered_files[ $tag ] ) ) {
  67.       return $this->registered_files[ $tag ];
  68.       }
  69.       return '';
  70.    }
  71.    Function ParseTemplate( $file ) {
  72.       if ( file_exists( $file ) ) {
  73.          $file_contents = '';
  74.          if ( $file_contents = join( '', @file( $file ) ) ) {
  75.             /* Pass 1 - Interpolate the variables */
  76.             /* Pass 1 Parse for registered_files */
  77.             if ( count( $this->registered_files ) > 0 ) {
  78.             reset( $this->registered_files );
  79.             while( list( $tag, $value ) = each( $this->registered_files ) ) {
  80.                if ( file_exists( $value ) ) {
  81.                   $included_file = ''; 
  82.                   if ( $included_file = join ( '', @file( $value ) ) ) {
  83.                      $file_contents = str_replace(
  84.                         $this->template_var_start .
  85.                         $tag .
  86.                         $this->template_var_end,
  87.                         $included_file,
  88.                         $file_contents
  89. //Simon/23may2000 - wtf, where'd this bs come from?
  90.                         //$value,
  91.                         //$included_file
  92.                      );
  93.                   }
  94.                }
  95.             }
  96.             }
  97.             /* Pass 2 - Interpolate the variables */
  98.             if ( count( $this->registered_tags ) > 0 ) {
  99.             reset( $this->registered_tags );
  100.             while( list( $tag, $value ) = each( $this->registered_tags ) ) {
  101.                $file_contents = str_replace(
  102.                   $this->template_var_start .
  103.                   $tag .
  104.                   $this->template_var_end,
  105.                   $value,
  106.                   $file_contents
  107.                );
  108.             }
  109.             }
  110.             if ( $this->with_php == 1 ) {
  111.                /* Eval is whacked in my mind */
  112.                eval( '?>' . $file_contents );
  113.             } else {
  114.                if ( $this->return_contents != 1 ) {
  115.                echo( $file_contents );
  116.                }
  117.             }
  118.             if ( $this->return_contents == 1 ) {
  119.                return $file_contents;
  120.             }
  121.             return 1;
  122.          } else {
  123.             return 0;
  124.          }
  125.       } else {
  126.          return 0;
  127.       }
  128.    }
  129. }
  130. ?>