-
Notifications
You must be signed in to change notification settings - Fork 65
/
SmartyView.class.php
73 lines (59 loc) · 1.93 KB
/
SmartyView.class.php
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
73
<?php
Library::import('recess.framework.views.AbstractView');
Library::import('recess.http.Response');
class SmartyView extends AbstractView {
protected $smarty = null;
public function __construct() {
Library::import('smarty.Smarty');
if($this->smarty !== ''){
$this->smarty = new Smarty();
}
$this->smarty->caching = 0;
$this->smarty->assign('url_base', $_ENV['url.base']);
$this->smarty->force_compile = Config::$mode == Config::DEVELOPMENT;
}
public function setTemplateDir($template_dir) {
$this->smarty->template_dir = $template_dir;
$this->smarty->cache_dir = $_ENV['dir.temp'] . 'smarty/cache/';
$this->smarty->compile_dir = $_ENV['dir.temp'] . 'smarty/templates_c/';
}
public function getTemplateDir() {
return $this->smarty->template_dir;
}
public function render(Response $response) {
$this->setTemplateDir($response->meta->viewDir);
switch($response->request->format) {
case Formats::json:
foreach($response->data as $key => $value) {
if($value instanceof ModelSet) {
$response->data[$key] = $value->toArray();
}
}
print json_encode($response->data);
exit;
default:
}
if($response->code == ResponseCodes::HTTP_CREATED) { exit; }
// TODO: Fill variables here
$this->smarty->assign('response', $response);
$this->smarty->assign('request', $response->request);
try {
foreach($response->data as $key => $value) {
$this->smarty->assign($key, $value);
}
} catch(Exception $e) {}
try {
$this->smarty->display($this->smarty->template_dir . $response->meta->viewName . '.tpl');
} catch(Exception $e) {
//print_r($e); exit;
if(!file_exists($this->smarty->cache_dir)) {
mkdir($this->smarty->cache_dir,null,true);
}
if(!file_exists($this->smarty->compile_dir)) {
mkdir($this->smarty->compile_dir,null,true);
}
$this->smarty->display($this->smarty->template_dir . $response->meta->viewName . '.tpl');
}
}
}
?>