-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.php
129 lines (104 loc) · 3.31 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
<?php
/**
* Class to instantiate view of controller/action
*
* @author Faizan Ayubi
*/
namespace Framework {
use Framework\Base as Base;
use Framework\Events as Events;
use Framework\Template as Template;
use Framework\View\Exception as Exception;
class View extends Base {
/**
* @readwrite
*/
protected $_file;
/**
* @readwrite
*/
protected $_data;
/**
* @read
*/
protected $_template;
/**
* Creates a Template instance, which it will later use to parse the view templates.
* it has methods for storing, retrieving, and erasing key/value pairs of template data, which it provides to the template parser.
* @param type $options
*/
public function __construct($options = array()) {
parent::__construct($options);
Events::fire("framework.view.construct.before", array($this->file));
$this->_template = new Template(array(
"implementation" => new Template\Implementation\Extended()
));
Events::fire("framework.view.construct.after", array($this->file, $this->template));
}
public function _getExceptionForImplementation($method) {
return new Exception\Implementation("{$method} method not implemented");
}
/**
* Parses the HTML file for the action
* @return string
*/
public function render() {
Events::fire("framework.view.render.before", array($this->file));
if (!file_exists($this->file)) {
return "";
}
return $this
->template
->parse(file_get_contents($this->file))
->process($this->data);
}
/**
* Get the variable defined in View instance
* @param type $key
* @param type $default
* @return type
*/
public function get($key, $default = "") {
if (isset($this->data[$key])) {
return $this->data[$key];
}
return $default;
}
protected function _set($key, $value) {
if (!is_string($key) && !is_numeric($key)) {
throw new Exception\Data("Key must be a string or a number");
}
$data = $this->data;
if (!$data) {
$data = array();
}
$data[$key] = $value;
$this->data = $data;
}
/**
* Set the variable defined in View instance
* @param type $key
* @param type $value
* @return \Framework\View
*/
public function set($key, $value = null) {
if (is_array($key)) {
foreach ($key as $_key => $value) {
$this->_set($_key, $value);
}
return $this;
}
$this->_set($key, $value);
return $this;
}
/**
* Erase the variable defined in View instance
* @param type $key
* @return \Framework\View
*/
public function erase($key) {
unset($this->data[$key]);
return $this;
}
}
}