juanc Posted April 20, 2006 Share Posted April 20, 2006 hi thereI was just experimenting with a template engine script I came across......I've found it works up to a point although I'm experiencing a problem..........The crux of it is that if you were to use an include file it's contents are outputted before any html .........then in the place holder where the contents should be a number "1" appears...........below is the code........I'm just hoping that it's something glaringly obvious that someone can quickly notice.........on the other hand it might be that the code is duff.......(btw.........here's the url for the original tutorial)[a href=\"http://www.tanru.com/php/replacement-template-engine.htm\" target=\"_blank\"]http://www.tanru.com/php/replacement-template-engine.htm[/a]This is the actual template engine script[code]<?phpclass template{var $ouput = NULL;function parseFile( $file ){if (file_exists($file)){$file = $file;}else{$file = "error.html";}return file_get_contents($file);}function parseTemplate( $templ, $vars ){$file = $this->parseFile($templ);foreach ($vars as $variable => $data){$variable = '{' . $variable . '}';$file = str_replace($variable, $data, $file);}$this->output = $file;}function display(){print $this->output;}}[/code]This is the instantiation of the classes object[code]<?phpinclude ("class_template.php");$data = array("TEST" => "This is a test","PHP_FILE" => include("wow.php"));$tpl = new template;$tpl->parseTemplate("test.tpl", $data);$tpl->display();?>[/code]Finally this is test.tpl the actual file for the layout...........note that the output of wow.php appears before anything else and the number "1" appears where you see {PHP_FILE}[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>{TEST}</title></head><body>{PHP_FILE}</body></html>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.