I'm creating a template engine to replace placeholder strings in my html template with dynamic content served by a function. To do this I have written the following template processor:
Code: Select all
function load_template()
{
$template_file = "template.html";
$template = fread(fopen($template_file, 'r'), filesize($template_file));
$template_vars = array('{MAIN}' => bodyContent(), '{NAV}' => navContent(), '{TITLE}' => $title);
function template_eval (&$template, &$template_vars)
{
return strtr($template, $template_vars);
}
echo template_eval($template, $template_vars);
}
load_template();Code: Select all
<?php
$title = "Example Title";
function navContent()
{
?>
<h1>Page Links:</h1>
<ul>
<li><a href="">Link 1</a></li>
<li><a href="">Link 2</a></li>
<li><a href="">Link 3</a></li>
</ul>
<?php
}
function bodyContent()
{
?>
<h1>Example Title 1</h1>
<p>Example Content 1</p>
<h1>Example Title 2</h1>
<p>Example Content 2</p>
<h1>Example Title 3</h1>
<p>Example Content 3</p>
<?php
}
require_once (URL."/test_templateProcessor.php");
?>I'm not quite sure why it does this, so any insight would be greatly appreciated. Thanks in advance for any and all help!
Jordan