2 define("DEBUG_INVIS", "1"); // inserts in the name of the template at the beginning of each in html comments turn off (set to 0) for non-debug # we need the following until we rebuild the whole system.. # it is because out dev and production servers are so different. # if the base path is defined, that means we are overriding this simple default.. $I_AM = $_SERVER['HTTP_HOST']; if(!defined('BASE_TEMPLATE_PATH')){ // could have been a switch, but this works fine for small sets.. if(substr_count($I_AM, "dev1.site.com")>0){ define("BASE_TEMPLATE_PATH", "/web/php/templates/dev1"); // gamma }elseif(substr_count($I_AM, "dev2.site.com")>0){ define("BASE_TEMPLATE_PATH", "/web/php/templates/dev2"); // gamma }else{ define("BASE_TEMPLATE_PATH", "/web/php/templates/mainsite"); //einstein }//fi }//fi /* about template() $arr is a set of values where the key(uppercase) is what is looked for and the value is the value to be replaced globally in the template. i.e.: $arr['BODY'] = "..."; will replace the value {BODY} in the template. $scheme is more for themes and are pretty easy to figure out. $text is a textual value to use instead of loading a file. $returns if set to 0 will cause the function to just echo the parsed template instead of returning it as a string. $base_override is to change the template base on the fly, this is useful for global templates. call like template($replacements, "", 1, "", "/directory/to/global/templates/_mainheader") setting $arr['template'] = "file.tpl" will cause the function to load sub-template file. this is generally for loading simple rows and tables that may be repeated to be tossed into the main template fuction. Was using eregi for replacements, but then ran some test over a 1000 iterations for a file and: speed in 3.12413215637 seconds for eregi speed in 1.04641199112 seconds for str_replace so obviously str_replace is the choice. this works like so: you check http://site.com/reports/report.php csr main file says BASE_TEMPLATE_PATH = /var/www/site/templates/main script_name returns /reports/report.php the base becomes /var/www/site/templates/main/reports/report.php/ for all templates relating to that page. */ function template($arr, $text="", $returns=1, $scheme="", $base_override=""){ global $DOCUMENT_ROOT, $PRELOADED; #$cwd = getcwd(); // for self contained within same directory, don't use for this $cwd = BASE_TEMPLATE_PATH; $caller = $_SERVER["SCRIPT_NAME"]; if($base_override <> ""){ $tpl_base = $base_override; // exact directory }else{ $tpl_base = $cwd . "$caller/"; // dynamic directory } $nodefaults = 1; #echo "\n"; if($text == ""){ // uncomment lines starting with ### to speed things up with in-memory templates cache // this saves the site from reloading the template from disk each time. ###if($PRELOADED[$tpl_base . $caller . $arr['template']] == ''){ // this is for sub-templates if($arr['template'] == ""){ $index = file_get_contents($tpl_base . $caller); }else{ $tpl_base = $tpl_base . $arr['template']; $tpl_alt = $cwd . "$caller/../" . $arr['template']; $root_tpl_alt = $cwd . "$caller/../../" . $arr['template']; if(file_exists($tpl_base)){ $index = file_get_contents($tpl_base); }elseif(file_exists($tpl_alt)){ $index = file_get_contents( $tpl_alt ); }elseif(file_exists($root_tpl_alt)){ $index = file_get_contents( $root_tpl_alt ); } unset($arr['template']); $nodefaults = 0; } ###}else{ ### $index = $PRELOADED[$tpl_base . $caller . $arr['template']]; ###} ###$PRELOADED[$tpl_base . $caller . $arr['template']] = $index; // load the tpl into memory }else{ $index = $text; } if($nodefaults == 1){ // some defaults for our index.. if($arr['CHARSET'] == ""){ $arr['CHARSET'] = 'iso-8859-1'; } if($arr['LANG'] == ""){ $arr['LANG'] = 'en'; } } if(is_array($scheme)){ while(list($key,$val)=each($scheme)){ $arr[COLORNAMEBASE . $key] = $val; } } /* NOW DO THE REPLACEMENTS */ if(is_array($arr)){ while(list($key,$val)=each($arr)){ $keys[] = "{".strtoupper($key)."}"; $vals[] = $val; //$index = eregi_replace("{[$key^}]+}", "$val", $index); //slower } $index = str_replace($keys, $vals, $index); // multiple action replace. } // template function replacements... if(function_exists("get_defined_functions")){ $def_funcs = get_defined_functions(); } while(list($key,$val)=each($def_funcs['user'])){ if(substr($val, 0, 4)=="tpl_"){ // function must start with tpl_ to be called $trans = array("{". strtoupper($val) ."}" => call_user_func($val)); // auto function calls $index = strtr($index, $trans); } } // this first one is not javascript safe.. #$index = eregi_replace("{[^}]+}", "", $index); // clear unset ones.. $index = eregi_replace("{[$^a-zA-Z0-9_$]+}", "", $index); // javascript safe clear unset. if(DEBUG_INVIS){ // this is handy for debugging.. $index = trim($index); $index = "\n\n $index \n\n"; } // and return if($returns){ // return the finished product as a string // useful for building templates within tempaltes return $index; }else{ // just dump out the finished product echo $index; } } // just sling it a result set :) // good for quick template buildings... function mysql_template($obj, $template, $text="", $returns=0){ while($list = mysql_fetch_assoc($obj)){ $parts = extract($list); $parts['template'] = $template; $ret .= template($parts, $text, $returns); } return $ret; } // just sling it a result sql statement :) // good for quick template buildings but needs an active sql connection. function sql_template($sql, $databasename, $template, $text="", $returns=0){ $results = mysql_db_query($databasename, $sql) or die("
TEMPLATE ERROR:
\n". mysql_error(). "\n
\nSQL:
\n" . $sql ."
"); while($list = mysql_fetch_assoc($results)){ $parts = extract($list); $parts['template'] = $template; $ret .= template($parts, $text, $returns); } return $ret; } //-------------------------------------------------- ------------------------------------------------ // functions for using php tags inside a template // i.e. "current time is date('Y-m-d',time())" // use 'after' running through template() so you can also add dynamic php in there.. // Time-stamp: <02 Feb 2006 10:27:58 joeldg> //-------------------------------------------------- ------------------------------------------------ function evalcode($matches){ eval("\\$matches[2] = $matches[2];"); return $matches[2]; } function php_template($input){ $output = preg_replace_callback("/()(.*)(<\\/php>)/U", "evalcode", $input); return $output; } /*************************************************************************** below should go into a file called _template_functions.php all internal template functions start with "tpl_" each function in two parts (two functions) tpl_FUNCTION_NAME with $numargs = func_num_args() and a if($numargs > 0){ then reset the function as needed if called by: tpl_FUNCTION_NAME_RESET neither should 'ever' need to have parameters passed to it these can be used to handle a lot of the repetetive tasks for checking logged-in status, row coloring etc.. ***************************************************************************/ $_GLOBAL_ROW_COLOR = ""; function tpl_rowcols(){ global $_GLOBAL_ROW_COLOR; $numargs = func_num_args(); $_GLOBAL_ROW_COLOR == "#FFFFFF" ? $_GLOBAL_ROW_COLOR = "#DDDDDD" : $_GLOBAL_ROW_COLOR = "#FFFFFF"; if($numargs > 0){ $_GLOBAL_ROW_COLOR = "#DDDDDD"; } return $_GLOBAL_ROW_COLOR; } function tpl_rowcols_reset(){ tpl_rowcols(1); return; } ?>