I've just completed coding my template engine. I'm willing to have a review on how to increase the quality, efficiency, elegance and performance of the code, perhaps by shortening the code, etc. I'm also open for suggestions on other ways of doing this. It would be really appreciated if you point out every tiny bit of things, as I am willing to modify and improve the code accordingly.
Model class:
<?php
class Model {
public $site_url;
public $theme;
public $params;
public function __construct($global) {
$this->site_url = $global['site_url'];
$this->theme = $global['theme'];
}
public function createParam($key, $value) {
$this->params['{' . $key . '}'] = $value;
}
public function replaceParams($content) {
foreach($params as $key => $value) {
str_replace($key, $value, $content);
}
return $content;
}
}
View class:
<?php
class View {
private $model;
private $controller;
public function __construct($model, $controller) {
$this->model = $model;
$this->controller = $controller;
}
public function handlePageLoad($page_name) {
$this->path = $model->site_url . "/app/tpl/skins/" . $model->theme . "/" . $page_name . ".php";
if($controller->pageExists($this->path) === true) {
$this->content = $controller->createPage($this->path);
return $this->content;
} else {
$this->newPath = $model->site_url . "/app/tpl/skins/" . $model->theme . "/404.php";
$this->content = $controller->createPage($this->path);
header('Location: ' . $model->site_url . "/index.php?page=404");
return $this->content;
}
}
}
Controller class:
<?php
class Controller {
private $model;
public function __construct($model) {
$this->model = $model;
}
public function pageExists($path) {
if(file_exists($path)) {
return true;
} else {
return false;
}
}
public function createPage($path) {
$this->content = $model->replaceParams(file_get_contents($path));
return $this->content;
}
}
Global.PHP
<?php
function __autoload($class_name) {
include "classes/class." . $class_name . ".php";
}
$global = array(
'site_url' = 'http://localhost/Projects/HassanCMS',
'theme' = 'v1'
);
$model = new Model($global);
$controller = new Controller($model);
$view = new View($model, $controller);
$model->createParam('site_title', 'HassanTech');
Modelclass is completely broken. PHP has no implicit$this, so none of the code there gets or sets anything on the object. \$\endgroup\$$property-- you have to say$this->property. So all the variables you ever set in that code, are local variables -- the code has no effect, and will probably emit notices. \$\endgroup\$