forked from 0oVicero0/oneindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.php
103 lines (81 loc) · 2.22 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
<?php
!defined('VIEW_PATH') && define('VIEW_PATH', 'view/');
class view {
private $_view = array('file' => '', 'data' => array());
static $_pos = null;
static $_section = null;
static function load($file, $set = null) {
if (is_int(strripos($file, '..'))) {
die("error view file name:$file");
}
$file = str_replace('.', '/', $file);
return new view($file, $set);
}
public function __construct($file, $set = null) {
if (isset(self::$_pos)) {
$this->_view['parent'] = self::$_pos;
$this->with($this->_view['parent']->_view['data']);
}
$this->_view['file'] = $file;
$this->with($set);
}
public function with($name, $value = NULL) {
if (is_array($name)) {
$this->_view['data'] = array_merge($this->_view['data'], $name);
} elseif (is_string($name)) {
$this->_view['data'][$name] = $value;
}
return $this;
}
public function __toString() {
self::$_pos = $this;
$this->_view['file'] = VIEW_PATH . $this->_view['file'] . '.php';
if(!file_exists($this->_view['file'])){
self::to404('404');
}
extract($this->_view['data']);
ob_start();
include $this->_view['file'];
if (isset($this->_view['layout'])) {
echo $this->_view['layout'] . ob_get_clean();
}
self::$_pos = isset($this->_view['parent']) ? $this->_view['parent'] : NULL;
$return_str = ob_get_clean();
return $return_str;
}
public function show() {
echo $this;
}
static function layout($file) {
ob_start();
$view_layout = self::load($file);
self::$_pos->_view['layout'] = $view_layout;
}
static function section($name) {
if (isset(self::$_pos->_view['section'][$name])) {
echo self::$_pos->_view['section'][$name];
} elseif (isset(self::$_pos->_view['parent']->_view['section'][$name])) {
echo self::$_pos->_view['parent']->_view['section'][$name];
}
}
static function begin($name) {
self::$_section = $name;
ob_start();
}
static function end() {
self::$_pos->_view['section'][self::$_section] = ob_get_clean();
self::$_section = null;
}
static function direct($loction) {
header("Location:$loction");
exit();
}
static function abort() {
header("HTTP/1.1 502 Bad Gateway");
exit();
}
static function to404(){
header("HTTP/1.1 404 Not Found");
exit();
}
}