All pastes #629559 Raw Edit

template generator example

public php v1 · immutable
·published 2007-07-22 12:46 UTC
rendered paste body
<?php /***  * Template Generator  *  * author: NovaKing (novaking@novaking.tv)  * modified: 2007-01-22  *  ***/    class Generator  {  	private $html_content;  	private $module_path;    	function __construct()  	{  		//  		global $site_root;  		$this->module_path = $site_root;  	}  	function Generator()  	{  		$this->__construct();  	}    	function PrepareTemplate( $template_name )  	{  		// Load the template file then parse for it's tags  		$template_filename = $this->module_path . '/hidden/templates/' . $template_name;    		if( @file_exists( $template_filename ) )  		{  			// Grab contents the parse for tags  			$this->html_content = @file_get_contents( $template_filename );  			$this->html_content = $this->ParseTags( $this->html_content );  			return true;  		}    		return ErrorMessage( 'Template file (' . basename( $template_name ) . ') does not exist.', __FILE__, __LINE__ );  	}    	function RenderOutput( $filename )  	{  		// Render the new template to a static page  		@file_put_contents( $filename, $this->html_content );  	}    	function ParseTags( $content )  	{  		$pattern = '|<!--([A-Z_]+)-->|i';    		if( @preg_match_all( $pattern, $content, $matches ) )  		{  			foreach( $matches[1] as $tag )  			{  				// Module exists, grab contenst and replace tag  				if( @file_exists( $this->module_path . '/modules/' . strtolower( $tag ) . '.inc.php' ) )  				{  					$tag_content = $this->GrabModuleContent( $this->module_path . '/modules/' . strtolower( $tag ) . '.inc.php', false );  					$content = str_replace( '<!--' . $tag . '-->', $tag_content, $content );  				}  				else if( @file_exists( $this->module_path . '/modules/' . strtolower( $tag ) . '.gen.php' ) )  				{  					$tag_content = $this->GrabModuleContent( $this->module_path . '/modules/' . strtolower( $tag ) . '.gen.php', true );  					$content = str_replace( '<!--' . $tag . '-->', $tag_content, $content );  				}  				else  				{  					Message( 'Module (' . $tag . ') does not exist, ignoring...', __FILE__, __LINE__ );  				}  			}  		}  		return $content;  	}    	// Grab contents from a module, and store the result in a variable  	function GrabModuleContent( $filename, $generate = false )  	{  		  		if( @file_exists( $filename ) )  		{  			// use ob to intercept include, and place data into  			// a variable to use in template  			if( $generate )  			{  				@ob_start();  					include $filename;  					$output = @ob_get_contents();  				@ob_end_clean();    				return $output;  			}  			else  			{  				return '<?php include \'' . $filename . '\'; ?>';  			}  		}  		return ErrorMessage( 'Module (' . basename( $filename ) . ') does not exist, can not parse...', __FILE__, __LINE__ );  	}  }?>