0
Hello, I had already posted here some time ago about my templates class, but I decided to a modified one, because every time I want to edit a menu for example, I have to edit in 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();
Note that I am 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}.");
}
}
}
However it always adds the last one that is my footer, there is some way to create a function or add without having to use the require
? 'Cause when I try to use the require
he doesn’t change my strings str_replace()
;
Well I tried to explain, I await answers.
I don’t understand Downvote?
– Guilherme SpinXO