php namespace Evilworlds View class Template private ctpp private base

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Evilworlds\View;
class Template {
private $ctpp;
private $base_path = '';
private $global_vars = [];
private static $instance = NULL;
public function __construct() {
$this -> ctpp = new \ctpp;
}
// Установить путь к шаблонам
public function setPath($base_path) {
$this -> base_path = $base_path;
}
// Установить глоьальные переменные
public function setGlobal($keys) {
foreach ($keys as $k => $v)
$this -> global_vars[$k] = $v;
return $this;
}
// Задать переменные
public function set($params) {
$this -> ctpp -> emit_params($params);
return $this;
}
// Отрендерить шаблон
public function render($file, $params = NULL, $base_path = true, $global_vars = true) {
\Evilworlds\Debug\Profiler::start("template_render", "Template::render()");
if ($base_path)
$file = $this -> base_path.'/'.$file.'.tmpl.ct2';
if ($params)
$this -> ctpp -> emit_params($params);
if ($this -> global_vars && $global_vars)
$this -> ctpp -> emit_params($this -> global_vars);
$code = $this -> ctpp -> load_bytecode($file);
$data = $this -> ctpp -> output_string($code);
$this -> ctpp -> free_bytecode($code);
\Evilworlds\Debug\Profiler::end("template_render");
//\Evilworlds\Debug\Profiler::end("template_render");
return $data;
}
// Сбросить переменные
public function reset() {
$this -> ctpp -> reset_params();
}
// Напечатать шаблон
public function display($file, $params = NULL, $base_path = true, $global_vars = true) {
echo $this -> render($file, $params, $base_path, $global_vars);
}
// Деструктор
public function __destruct() {
unset($this -> ctpp);
unset($this -> global_vars);
}
// Singleton
public static function getInstance() {
if (self::$instance === NULL) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
}