-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.php
145 lines (129 loc) · 3.21 KB
/
View.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* DframeFramework
* Copyright (c) Sławomir Kaleta.
*
* @license https://github.com/dframe/dframe/blob/master/LICENCE (MIT)
*/
namespace Dframe\View;
use Dframe\Loader\Loader;
use Dframe\Router\Response;
use Dframe\View\Exceptions\ViewException;
/**
* View Class.
*
* @author Sławomir Kaleta <slaszka@gmail.com>
*/
abstract class View extends Loader implements ViewInterface
{
/**
* Path Templates
*
* @var string
*/
public $dir;
/**
* Defines template variables.
*
* @param string $name
* @param mixed $value
*
* @return mixed
* @throws ViewException
*/
public function assign($name, $value)
{
if (!isset($this->view)) {
throw new ViewException('Please Define view engine in app/View.php', 500);
}
return $this->view->assign($name, $value);
}
/**
* Generates the output of the templates with parsing all the template variables.
*
* @param string|array $data
* @param string $type
*
* @return mixed
* @throws ViewException
*/
public function render($data, $type = null)
{
if (empty($type) or $type === 'html') {
return Response::Create($this->renderInclude($data));
} elseif ($type === 'jsonp') {
return $this->renderJSONP($data);
} else {
return $this->renderJSON($data);
}
}
/**
* File including
*
* @param string $name
* @param null|string $path
*
* @return mixed
* @throws ViewException
*/
public function renderInclude($name, $path = null)
{
if (!isset($this->view)) {
throw new ViewException('Please Define view engine in app/View.php', 500);
}
if (!is_null($this->dir)) {
$this->view->setTemplateDir($this->dir);
}
return $this->view->renderInclude($name, $path);
}
/**
* Display JSONP.
*
* @param array $data
*/
public function renderJSONP($data)
{
$callback = null;
if (isset($_GET['callback'])) {
$callback = $_GET['callback'];
}
exit(
Response::Create($callback . '(' . json_encode($data) . ')')->headers(
['Content-Type' => 'application/jsonp']
)->display()
);
}
/**
* Display JSON.
*
* @param array $data
* @param int $status
*/
public function renderJSON($data, $status = 200)
{
exit(
Response::Create(json_encode($data))->status($status)->headers(
['Content-Type' => 'application/json']
)->display()
);
}
/**
* Fetch the output of the templates with parsing all the template variables.
*
* @param string $name
* @param string $path
*
* @return mixed
* @throws ViewException
*/
public function fetch($name, $path = null)
{
if (!isset($this->view)) {
throw new ViewException('Please Define view engine in app/View.php', 500);
}
if (!is_null($this->dir)) {
$this->view->setTemplateDir($this->dir);
}
return $this->view->fetch($name, $path);
}
}