Jump to content

template engine script abit buggy


juanc

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.