note 23134 deleted from function.eval by victor

From: Date: Sat, 12 Jun 2004 22:40:22 +0000
Subject: note 23134 deleted from function.eval by victor
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71001@lists.php.net to get a copy of this message
Note Submitter: cyril@cylman.com ---- Eval is great for making templates with arrays, I use 2 functions // function to parse a template string with eval function cap_parse($string) { global $cap_tpl; $string=addslashes($string); $string=ereg_replace("{__","\".\$cap_tpl[\"_",$string); $string=ereg_replace("__}","_\"].\"",$string); eval ("\$string = \"$string\";"); return $string; } // function to parse a template file with eval function cap_parse_tpl($file) { global $cap_tpl; $string = implode ('', file ("$file")); $string=cap_parse($string); return $string; } Here a small exemple of template with eval : make a table with several rows from a result with 2 templates files // in file : opentable.tpl <table>{__CAPTR__}</table> // end of file : opentable.tpl // in file : trtable.tpl <tr><td>{__CAPNAME__}</td></tr> // end of file : trtable.tpl // in file : index.php // the following lines could be in a loop fetching the result of an sql query ... while($res=select_query($result)) { $cap_tpl['_CAPNAME_']="$res[1]"; $cap_tpl['_CAPTR_']= $cap_tpl['_CAPTR_'].cap_parse_tpl("trtable.tpl"); } // now the $cap_tpl['_CAPTR_'] contains the rows of the table echo cap_parse_tpl("opentable.tpl"); // end of file : index.php that's all

« previous php.notes (#71001) next »