if(!function_exists("file_get_contents")){ function file_get_contents($f) { if (file_exists($f)) { return implode('', file($f)); } else { return 0; // ("File does not exist!"); } } }//fi // fast/simple/powerful templating // developed by Joel De Gan - http://blog.peoplesdns.com // // various nice color schemes we can use... these are muted colors.. // from 0, item 5 is the lightest, the last is the darkest and the first is the base // see COLORNAMEBASE below for usage. $scheme_1 = array( "6F7F06", "939F45", "B8C083", "DBDFC1", "F1F3E7", "5E6C05", "535F05", "384003", "1C2002", "0B0D01"); $scheme_2 = array( "A2685C", "BA8E85", "D1B4AE", "E8D9D6", "F6F0EF", "8A584E", "7A4E45", "51342E", "291A17", "100A09"); $scheme_3 = array( "98787B", "B29A9C", "CCBCBE", "E5DDDE", "F5F2F2", "816669", "725A5C", "4C3C3E", "261E1F", "0F0C0C"); $scheme_4 = array( "C0D88C", "D0E2A9", "E0ECC6", "EFF5E2", "F9FCF4", "A3B877", "90A269", "606C46", "303623", "13160E"); $scheme_5 = array( "657D11", "8C9E4D", "B3BF89", "D8DEC3", "F0F3E8", "566A0E", "4C5E0D", "333F09", "191F04", "0A0D02"); $scheme_6 = array( "5B6113", "84894E", "AEB18A", "D6D7C4", "EFF0E8", "4D5210", "44490E", "2E310A", "171805", "090A02"); $scheme_7 = array( "C1A93C", "D1BF6D", "E1D59E", "EFE9CE", "F9F7EC", "A49033", "917F2D", "61551E", "302A0F", "131106"); $scheme_8 = array( "BE8B91", "CFA8AD", "DFC6C9", "EFE2E3", "F9F4F5", "A2767B", "8F686D", "5F4649", "302324", "130E0F"); $scheme_9 = array( "849D5E", "A3B687", "C2CFAF", "E0E6D7", "F3F6EF", "708550", "637647", "424F2F", "212718", "0D1009"); $scheme_10 = array( "97A356", "B1BA81", "CCD2AB", "E5E8D5", "F5F6EF", "808B49", "717A41", "4C522B", "262916", "0F1009"); define("COLORNAMEBASE", "THEME_COL_"); // if you passed in $scheme_2 into template then THEME_COL_2 would be D1B4AE ^^ row 2, count 0,1 => 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:"); 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
\n". mysql_error(). "\n
\nSQL:
\n" . $sql ."