I am using the following to replace curly bracket markers in .tpl
files, sort of a quasi templating engine. I do not really want the overhead of having a template engine when I just need it for these three simple things. The problem I am having is that when I call a function that generates for example a nav (buildNav
), when the page renders they for some reason parse at the top of the page, and not within the curly bracket area where they should be. What is going on and how can I fix it? For reference I am referring to the functions in the array of body
.
<?php
class Template
{
public $path;
public $ext = '.tpl';
public function __construct()
{
$this->path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
public function head($title = '')
{
$title = isset($title) ? $title : 'NEOU CMS';
$replace = array(
"page_title" => $title,
"JQUERY_UI_CSS" => JQUERY_UI_CSS,
"JQUERY_JS" => JQUERY_JS,
"JQUERY_UI_JS" => JQUERY_UI_JS
);
echo self::parse_template('header', $replace);
}
public function body()
{
require('customize.php');
$image = new Images();
$replace = array(
"profile_picture" => $image->profilePic(),
"navigation" => buildNav()
);
echo self::parse_template('body', $replace);
}
public function footer()
{
$replace = array(
"date" => date("Y")
);
echo self::parse_template('footer', $replace);
}
/**
* @param string $filename
* @param array $data
*/
public function parse_template($filename, $data)
{
$out = file_get_contents($this->path . $filename . $this->ext);
if (is_array($data)) {
foreach ($data as $key => $value) {
$out = str_replace('{' . $key . '}', $value, $out);
}
}
return $out;
}
}
?>