php Class Template private template private controller private layouts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
Class Template {
private $template;
private $controller;
private $layouts;
private $vars = array();
function __construct($layouts, $controllerName) {
$this->layouts = $layouts;
$arr = explode('_', $controllerName);
$this->controller = strtolower($arr[1]);
}
function vars($varname, $value) {
if (isset($this->vars[$varname]) == true) {
trigger_error ('Unable to set var `' . $varname . '`. Already set, and overwrite not allowed.', E_USER_NOTICE);
return false;
}
$this->vars[$varname] = $value;
return true;
}
function view($name, $title, $controller = null) {
$pathLayout = SITE_PATH . 'views' . DS . 'layouts' . DS . $this->layouts . '.php';
$controller = $controller != null ? $controller : $this->controller;
$contentPage = SITE_PATH . 'views' . DS . $name . DS . $controller . '.php';
if (file_exists($pathLayout) == false) {
trigger_error ('Layout `' . $this->layouts . '` does not exist.', E_USER_NOTICE);
return false;
}
if (file_exists($contentPage) == false) {
trigger_error ('Template `' . $name . '` does not exist.', E_USER_NOTICE);
return false;
}
$this->vars[count($this->vars)+1] = ["title" => $title];
foreach ($this->vars as $key => $value) {
$$key = $value;
}
include ($pathLayout);
}
}