Jump to content

Archived

This topic is now archived and is closed to further replies.

juanc

template engine script abit buggy

Recommended Posts

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

[code]
<?php
class 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]

<?php
include ("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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.