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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
<?php
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
namespace flight\template;
/**
* The View class represents output to be displayed. It provides
* methods for managing view data and inserts the data into
* view templates upon rendering.
*/
class View {
/**
* Location of view templates.
*
* @var string
*/
public $path;
/**
* View variables.
*
* @var array
*/
protected $vars = array();
/**
* Template file.
*
* @var string
*/
private $template;
/**
* Constructor.
*
* @param string $path Path to templates directory
*/
public function __construct($path = '.') {
$this->path = $path;
}
/**
* Gets a template variable.
*
* @param string $key Key
* @return mixed Value
*/
public function get($key) {
return isset($this->vars[$key]) ? $this->vars[$key] : null;
}
/**
* Sets a template variable.
*
* @param mixed $key Key
* @param string $value Value
*/
public function set($key, &$value = null) {
if (is_array($key) || is_object($key)) {
foreach ($key as $k => &$v) {
$this->vars[$k] = $v;
}
}
else {
$this->vars[$key] = &$value;
}
}
/**
* Checks if a template variable is set.
*
* @param string $key Key
* @return boolean If key exists
*/
public function has($key) {
return isset($this->vars[$key]);
}
/**
* Unsets a template variable. If no key is passed in, clear all variables.
*
* @param string $key Key
*/
public function clear($key = null) {
if (is_null($key)) {
$this->vars = array();
}
else {
unset($this->vars[$key]);
}
}
/**
* Renders a template.
*
* @param string $file Template file
* @param array $data Template data
* @throws \Exception If template not found
*/
public function render($file, $data = null) {
$this->template = $this->getTemplate($file);
if (!file_exists($this->template)) {
throw new \Exception("Template file not found: {$this->template}.");
}
if (is_array($data)) {
$this->vars = array_merge($this->vars, $data);
}
extract($this->vars);
include $this->template;
}
/**
* Gets the output of a template.
*
* @param string $file Template file
* @param array $data Template data
* @return string Output of template
*/
public function fetch($file, $data = null) {
ob_start();
$this->render($file, $data);
$output = ob_get_clean();
return $output;
}
/**
* Checks if a template file exists.
*
* @param string $file Template file
* @return bool Template file exists
*/
public function exists($file) {
return file_exists($this->getTemplate($file));
}
/**
* Gets the full path to a template file.
*
* @param string $file Template file
* @return string Template file location
*/
public function getTemplate($file) {
if ((substr($file, -4) != '.php')) {
$file .= '.php';
}
if ((substr($file, 0, 1) == '/')) {
return $file;
}
else
{
return $this->path.'/'.$file;
}
}
/**
* Displays escaped output.
*
* @param string $str String to escape
* @return string Escaped string
*/
public function e($str) {
echo htmlentities($str);
}
}
|