Web Hosting Talk







View Full Version : PHP Template Class that Parses PHP Code


blackworm
08-10-2004, 09:46 PM
Without using Smarty, does anyone have any ideas how it can be done?

Right now the templating system I have will just output PHP code as HTML, I want it to run the PHP code and process it as well.

Burhan
08-11-2004, 02:53 AM
http://www.php.net/eval -- be very, very, very, careful how you use it.


Did I mention be careful?

JimPanse
08-11-2004, 03:32 AM
i wrote this lil thingy... well it uses files instead of mysql in this case but you could change this for you needs.


function gettemplate($template){

STATIC $templates;
$relpath = THIS_ROOT . "/templates/";

if(!$ext = strpos($template,".")){
echo("Error: Function gettemplate($template). No valid filename!<br>");
return false;
}
$cache = substr($template,0,$ext);

if(!$templates[$cache]){
if(!file_exists($relpath.$template)){
echo("Error: Function gettemplate($template). No templatefile found in path \"$relpath\" !<br>");
return Null;
} else {
$template = implode("",file($relpath.$template));
$template = addslashes($template);
$templates[$cache] = str_replace("\\'","'",$template);
}
}
return $templates[$cache];
}



useage:


eval("echo (\"".gettemplate("yourtemplatefile.tpl")."\");");


njoy

blackworm
08-11-2004, 08:20 AM
Yeah, so far eval is the only way I've found that works.

Thanks for the input, much oblidged

cblc3kw
08-14-2004, 01:08 PM
Yes eval is the way to go for you but it can be a little difficult to manage your application with the eval as it is bit more difficult and messy than say Smarty.