Login   Register  
PHP Classes
elePHPant
Icontem

File: htmltemplate.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Hiroshi  >  HTML Template  >  htmltemplate.inc  >  Download  
File: htmltemplate.inc
Role: ???
Content type: text/plain
Description: HTML template
Class: HTML Template
Simple HTML template class
Author: By
Last change:
Date: 2002-03-19 14:30
Size: 4,708 bytes
 

Contents

Class file image Download
<?
/**
* HTML Template
* License: BSD

The instruction is here;
http://hoover.ktplan.ne.jp/kaihatsu/php_en/index.php

**/

class HtmlTemplate{
	

	function t_Include($file,$data,$dirname="./"){
		HtmlTemplate::_htmltmp2($file,$data,$dirname);
	}

	function t_Buffer($file,$data,$dirname="./"){
		flush();
		ob_start();
		HtmlTemplate::_htmltmp2($file,$data,$dirname);
		$ans=ob_get_contents();
		ob_end_clean();
		return $ans;
	}

	function _htmltmp2($file,$data,$dirname){
		$val=$data;
		$t=ereg_replace("html?$","tmp",$file);
		if(substr($dirname,-1)!="/") $dirname.="/";
		if(!file_exists($dirname.dirname($t))) HtmlTemplate::_ForceDirectories($dirname.dirname($t));
		if(file_exists($dirname.$t)){
			if(filemtime($file)>filemtime($dirname.$t)) HtmlTemplate::_compile($file,$dirname);
		}else HtmlTemplate::_compile($file,$dirname);
			include($dirname.$t);
	}


	function _ForceDirectories($path,$mode=0777) {
		if ( strlen($path) == 0) return 0;
		//
		if ( strlen($path) < 3) return 1; // avoid 'xyz:\' problem.
		elseif ( is_dir($path)) return 1; // avoid 'xyz:\' problem.
		elseif   ( dirname($path) == $path) return 1; // avoid 'xyz:\' problem.
		return ( HtmlTemplate::_ForceDirectories(dirname($path),$mode) and @mkdir( $path, $mode));
	}
  

	function _compile($tmpfile,$dirname="./"){
		$tm=split("/",$tmpfile);
		if(file_exists($tmpfile) && ereg("\\.htm.?$",$tmpfile)){
			$newfile=$dirname.ereg_replace("\\.htm.?$",".tmp",$tmpfile);
			$all=fread(fopen($tmpfile,"rb"),filesize($tmpfile));
			$fp=fopen($newfile,"w");
			fwrite($fp,HtmlTemplate::_parsesrc($all));
			ftruncate($fp,ftell($fp));
			fclose($fp);
		}
	}


	function _parsesrc($str){
		$str=ereg_replace("\r\n","\n",$str);
		
		$kuri=array();
		$str2=$str;
		$acc=1;
		
		$kuri=array();
		preg_match_all("/<!--[\s\r\n\t]*\{[\s\r\n\t]*each[\s\r\n\t]+([^\}]+)[\s\r\n\t]*}[\s\r\n\t]*-->/i",$str2,$k,PREG_SET_ORDER);
		while(list(,$x)=each($k)){
			$kuri[]=$x[1];
		}
		while(list(,$m)=each($kuri)){
			$ar=split("/",$m);
			$ind="";
			$rui=array();
			$mattan=0;
			while(list(,$x)=each($ar)){
				array_push($rui,$x);
				if($mattan!=count($ar)-1 && in_array(join("/",$rui),$kuri)) {$ind.="[$x][\$cmpl_i[".join("_",$rui)."]]";}
				else {$ind.="[$x]";}
				$mattan++;
			}
			$n=ereg_replace("/","_",$m);
			$str2=preg_replace('/<!--[\s\r\n\t]*\{[\s\r\n\t]*each\s+'.ereg_replace("/","\\/",preg_quote($m)).'[\s\r\n\t]*\}[\s\r\n\t]*-->/i',
			"<?php
			for(\$cmpl_i[$n]=0;\$cmpl_i[$n]<count(\$val$ind);\$cmpl_i[$n]++){
				?>",
			$str2);
		}
		reset($kuri);

		$str2=preg_replace("/<!--[\s\r\n\t]*\{[\s\r\n\t]*\/each[\s\r\n\t]*\}[\s\r\n\t]*-->/i",
		"<?php
		}
		?>",$str2);

		while(preg_match('/\{[\s\r\n\t]*val[\s\r\n\t]+([^\}]+)[\s\r\n\t]*\}/',$str2,$match)){
			$m=$match[1];
			$ar=split("/",$m);
			$ind="";
			$rui=array();
			while(list(,$x)=each($ar)){
				array_push($rui,$x);
				if(in_array(join("/",$rui),$kuri)){ $ind.="[".$x."][\$cmpl_i[".join("_",$rui)."]]";}
				else {$ind.="[".$x."]";}
			}
			
			$str2=preg_replace('/\{[\s\r\n\t]*val[\s\r\n\t]+'.ereg_replace("/","\\/",preg_quote($m)).'[\s\r\n\t]*\}/i',
			"<?php print nl2br(\$val$ind); ?>",
			$str2);
		}

		while(preg_match('/\{[\s\r\n\t]*rval[\s\r\n\t]+([^\}]+)[\s\r\n\t]*\}/',$str2,$match)){
			$m=$match[1];
			$ar=split("/",$m);
			$ind="";
			$rui=array();
			while(list(,$x)=each($ar)){
				array_push($rui,$x);
				if(in_array(join("/",$rui),$kuri)){ $ind.="[".$x."][\$cmpl_i[".join("_",$rui)."]]";}
				else {$ind.="[".$x."]";}
			}
			
			$str2=preg_replace('/\{[\s\r\n\t]*rval[\s\r\n\t]+'.ereg_replace("/","\\/",preg_quote($m)).'[\s\r\n\t]*\}/i',
			"<?php print \$val$ind; ?>",
			$str2);
		}


		while(preg_match('/<!--[\s\r\n\t]*\{[\s\r\n\t]*def[\s\r\n\t]+([^\}]+)[\s\r\n\t]*}[\s\r\n\t]*-->/i',$str2,$match)){
			$m=$match[1];
			$ar=split("/",$m);
			$ind="";
			$rui=array();
			$mattan=0;
			while(list(,$x)=each($ar)){
				array_push($rui,$x);
				if($mattan!=count($ar)-1 && in_array(join("/",$rui),$kuri)) {$ind.="[".$x."][\$cmpl_i[".join("_",$rui)."]]";}
				else {$ind.="[".$x."]";}
				$mattan++;
			}
			$str2=preg_replace("/<!--[\s\r\n\t]*\{def[\s\r\n\t]+".ereg_replace("/","\\/",preg_quote($m))."\}[\s\r\n\t]*-->/i",
			"<?php
			if((gettype(\$val$ind)!='array' && \$val$ind!=\"\") or (gettype(\$val$ind)=='array' && count(\$val$ind)>0)){
				?>",
			$str2);
		}
		$str2=preg_replace("/<!--[\s\r\n\t]*\{[\s\r\n\t]*\/def[\s\r\n\t]*\}[\s\r\n\t]*-->/i",
		"<?php
		}
		?>",
		$str2);
		$str2=preg_replace("/\?>[\s\r\n\t]*<\?php/","",$str2);
		
		
		return $str2;
	}

}

?>