TemplateObject.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:4k
- <?php
- class TemplateObject extends BaseObject {
- var $registered_tags;
- var $registered_files;
- var $template_var_start;
- var $template_var_end;
- var $with_php;
- var $return_contents;
- Function TemplateObject() {
- $this->BaseObject( 'TemplateObject' );
- $this->registered_tags = Array();
- $this->registered_files = Array();
- $this->template_var_start = '[-';
- $this->template_var_end = '-]';
- $this->with_php = 0;
- $this->return_contents = 0;
- }
- Function Register( $tag, $value ) {
- if ( isset( $this->registered_files[ $tag ] ) ) {
- return false;
- } else {
- $this->registered_tags[ $tag ] = $value;
- return true;
- }
- }
- Function RegisterFile( $tag, $value ) {
- if ( isset( $this->registered_files[ $tag ] ) ) {
- return false;
- } else {
- $this->registered_files[ $tag ] = $value;
- return true;
- }
- }
- Function UnRegister( $tag ) {
- $t_array = Array();
- $success = 0;
- reset( $this->registered_tags );
- while( list( $key, $value ) = each( $this->registered_tags ) ) {
- if ( $key != $tag ) {
- $t_array[ $key ] = $value;
- } else {
- $success = 1;
- }
- }
- $this->registered_tags = $t_array;
- return $success;
- }
- Function UnRegisterFile( $tag ) {
- $t_array = Array();
- $success = 0;
- reset( $this->registered_files );
- while( list( $key, $value ) = each( $this->registered_files ) ) {
- if ( $key != $tag ) {
- $t_array[ $key ] = $value;
- } else {
- $success = 1;
- }
- }
- $this->registered_files = $t_array;
- return $success;
- }
- Function GetValue( $tag ) {
- if ( isset( $this->registered_tags[ $tag ] ) ) {
- return $this->registered_tags[ $tag ];
- }
- if ( isset( $this->registered_files[ $tag ] ) ) {
- return $this->registered_files[ $tag ];
- }
- return '';
- }
- Function ParseTemplate( $file ) {
- if ( file_exists( $file ) ) {
- $file_contents = '';
- if ( $file_contents = join( '', @file( $file ) ) ) {
- /* Pass 1 - Interpolate the variables */
- /* Pass 1 Parse for registered_files */
- if ( count( $this->registered_files ) > 0 ) {
- reset( $this->registered_files );
- while( list( $tag, $value ) = each( $this->registered_files ) ) {
- if ( file_exists( $value ) ) {
- $included_file = '';
- if ( $included_file = join ( '', @file( $value ) ) ) {
- $file_contents = str_replace(
- $this->template_var_start .
- $tag .
- $this->template_var_end,
- $included_file,
- $file_contents
- //Simon/23may2000 - wtf, where'd this bs come from?
- //$value,
- //$included_file
- );
- }
- }
- }
- }
- /* Pass 2 - Interpolate the variables */
- if ( count( $this->registered_tags ) > 0 ) {
- reset( $this->registered_tags );
- while( list( $tag, $value ) = each( $this->registered_tags ) ) {
- $file_contents = str_replace(
- $this->template_var_start .
- $tag .
- $this->template_var_end,
- $value,
- $file_contents
- );
- }
- }
- if ( $this->with_php == 1 ) {
- /* Eval is whacked in my mind */
- eval( '?>' . $file_contents );
- } else {
- if ( $this->return_contents != 1 ) {
- echo( $file_contents );
- }
- }
- if ( $this->return_contents == 1 ) {
- return $file_contents;
- }
- return 1;
- } else {
- return 0;
- }
- } else {
- return 0;
- }
- }
- }
- ?>