note 23134 added to function.eval

From: Date: Wed, 10 Jul 2002 14:03:20 +0000
Subject: note 23134 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-32837@lists.php.net to get a copy of this message
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 -- http://www.php.net/manual/en/function.eval.php http://master.php.net/manage/user-notes.php?action=edit+23134 http://master.php.net/manage/user-notes.php?action=delete+23134 http://master.php.net/manage/user-notes.php?action=reject+23134

« previous php.notes (#32837) next »