I want to separate the output of HTML from my program code in my projects, so I wrote a very simple database class.
<?php
class Template
{
private $template;
function load($filePath)
{
if(!$this->template = file_get_contents($filePath))
$this->error('Error: Failed to open <strong>' . $filePath . '</strong>');
}
function replace($var, $content)
{
$this->template = str_replace("{$var}", $content, $this->template);
}
function display()
{
echo $this->template;
}
function error($errorMessage)
{
die('die() by template class: <strong>' . $errorMessage . '</strong>');
}
}
?>
The thing I need help with is the display()
method. Say for example I use this code:
$tplObj = new Template();
$tplObj->load('index.php');
$tplObj->replace('{TITLE}', 'Homepage');
$tplObj->display();
And the index.php
file is this:
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
<h1>{TITLE}</h1>
<?php
if($something) {
echo '$something is true';
} else {
echo '$something is false';
}
?>
</body>
</html>
I'm just wondering if the PHP code in there would be run? Or would it just be sent to the browser as plaintext? I was using eval()
in my template class but I hate that function :P
Thanks.