Jump to content


template engine script abit buggy

  • Please log in to reply
No replies to this topic

#1 juanc

  • Members
  • PipPip
  • Member
  • 16 posts

Posted 20 April 2006 - 07:16 PM

hi there

I 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

class template

var $ouput = NULL;

function parseFile( $file )

if (file_exists($file))

$file = $file;


$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;



This is the instantiation of the classes object

include ("class_template.php");

$data = array("TEST" => "This is a test",
"PHP_FILE" => include("wow.php"));

$tpl = new template;

$tpl->parseTemplate("test.tpl", $data);



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}

<!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">

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users