Hello, I had posted here for some time about my template class, but I decided to modify it, because every time I want to edit a menu, for example, I have to edit all my files.
This is my class
class Template {
private $_template;
private $_assign = array();
public function set($file) {
$path = './templates/' . DEFAULT_THEME . '/header.tpl.php';
$path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
$path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';
if (!empty($path)) {
if (file_exists($path)) {
$this->_template = file_get_contents($header);
$this->_template = file_get_contents($path);
$this->_template = file_get_contents($footer);
} else {
die("Template error: file not found in: {$path}.");
}
}
}
public function assign($string_search, $string_replace) {
if (!empty($string_search)) {
$this->_assign[strtoupper($string_search)] = $string_replace;
}
}
public function display() {
if (count($this->_assign) > 0) {
foreach ($this->_assign as $key => $value) {
$this->_template = str_replace('{' . $key . '}', $value, $this->_template);
}
}
return $this->_template;
}
}
And I use it like this:
<?php
require './includes/configs/Configs.php';
require './includes/autoload/autoload.php';
$template = new Template();
$template->set('home');
$template->assign('home', 'this is home');
echo $template->display();
Notice that I'm trying to include my header and footer in the template class:
public function set($file) {
$path = './templates/' . DEFAULT_THEME . '/header.tpl.php';
$path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
$path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';
if (!empty($path)) {
if (file_exists($path)) {
$this->_template = file_get_contents($header);
$this->_template = file_get_contents($path);
$this->_template = file_get_contents($footer);
} else {
die("Template error: file not found in: {$path}.");
}
}
}
But it always adds the last one that is my footer, is there any way to create a function or add it without having to use require
? When I try to use require
it does not change my strings str_replace()
;
Well, I've tried to explain, I'm waiting for answers.