bzr branch
http://bzr.ed.am/www/slight
1
by Tim Marston
initial commit |
1 |
<?php |
2 |
/** |
|
3 |
* Flight: An extensible micro-framework. |
|
4 |
* |
|
5 |
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com> |
|
6 |
* @license MIT, http://flightphp.com/license |
|
7 |
*/ |
|
8 |
||
9 |
namespace flight\template; |
|
10 |
||
11 |
/** |
|
12 |
* The View class represents output to be displayed. It provides |
|
13 |
* methods for managing view data and inserts the data into |
|
14 |
* view templates upon rendering. |
|
15 |
*/ |
|
16 |
class View { |
|
17 |
/** |
|
18 |
* Location of view templates. |
|
19 |
* |
|
20 |
* @var string |
|
21 |
*/ |
|
22 |
public $path; |
|
23 |
||
24 |
/** |
|
25 |
* View variables. |
|
26 |
* |
|
27 |
* @var array |
|
28 |
*/ |
|
29 |
protected $vars = array(); |
|
30 |
||
31 |
/** |
|
32 |
* Template file. |
|
33 |
* |
|
34 |
* @var string |
|
35 |
*/ |
|
36 |
private $template; |
|
37 |
||
38 |
/** |
|
39 |
* Constructor. |
|
40 |
* |
|
41 |
* @param string $path Path to templates directory |
|
42 |
*/ |
|
43 |
public function __construct($path = '.') { |
|
44 |
$this->path = $path; |
|
45 |
} |
|
46 |
||
47 |
/** |
|
48 |
* Gets a template variable. |
|
49 |
* |
|
50 |
* @param string $key Key |
|
51 |
* @return mixed Value |
|
52 |
*/ |
|
53 |
public function get($key) { |
|
54 |
return isset($this->vars[$key]) ? $this->vars[$key] : null; |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* Sets a template variable. |
|
59 |
* |
|
60 |
* @param mixed $key Key |
|
61 |
* @param string $value Value |
|
62 |
*/ |
|
63 |
public function set($key, &$value = null) { |
|
64 |
if (is_array($key) || is_object($key)) { |
|
3
by Tim Marston
flight: set only references to variables in views |
65 |
foreach ($key as $k => &$v) { |
1
by Tim Marston
initial commit |
66 |
$this->vars[$k] = $v; |
67 |
} |
|
68 |
} |
|
69 |
else { |
|
3
by Tim Marston
flight: set only references to variables in views |
70 |
$this->vars[$key] = &$value; |
1
by Tim Marston
initial commit |
71 |
} |
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Checks if a template variable is set. |
|
76 |
* |
|
77 |
* @param string $key Key |
|
78 |
* @return boolean If key exists |
|
79 |
*/ |
|
80 |
public function has($key) { |
|
81 |
return isset($this->vars[$key]); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Unsets a template variable. If no key is passed in, clear all variables. |
|
86 |
* |
|
87 |
* @param string $key Key |
|
88 |
*/ |
|
89 |
public function clear($key = null) { |
|
90 |
if (is_null($key)) { |
|
91 |
$this->vars = array(); |
|
92 |
} |
|
93 |
else { |
|
94 |
unset($this->vars[$key]); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Renders a template. |
|
100 |
* |
|
101 |
* @param string $file Template file |
|
102 |
* @param array $data Template data |
|
103 |
* @throws \Exception If template not found |
|
104 |
*/ |
|
105 |
public function render($file, $data = null) { |
|
106 |
$this->template = $this->getTemplate($file); |
|
107 |
||
108 |
if (!file_exists($this->template)) { |
|
109 |
throw new \Exception("Template file not found: {$this->template}."); |
|
110 |
} |
|
111 |
||
112 |
if (is_array($data)) { |
|
113 |
$this->vars = array_merge($this->vars, $data); |
|
114 |
} |
|
115 |
||
116 |
extract($this->vars); |
|
117 |
||
118 |
include $this->template; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Gets the output of a template. |
|
123 |
* |
|
124 |
* @param string $file Template file |
|
125 |
* @param array $data Template data |
|
126 |
* @return string Output of template |
|
127 |
*/ |
|
128 |
public function fetch($file, $data = null) { |
|
129 |
ob_start(); |
|
130 |
||
131 |
$this->render($file, $data); |
|
132 |
$output = ob_get_clean(); |
|
133 |
||
134 |
return $output; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Checks if a template file exists. |
|
139 |
* |
|
140 |
* @param string $file Template file |
|
141 |
* @return bool Template file exists |
|
142 |
*/ |
|
143 |
public function exists($file) { |
|
144 |
return file_exists($this->getTemplate($file)); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Gets the full path to a template file. |
|
149 |
* |
|
150 |
* @param string $file Template file |
|
151 |
* @return string Template file location |
|
152 |
*/ |
|
153 |
public function getTemplate($file) { |
|
154 |
if ((substr($file, -4) != '.php')) { |
|
155 |
$file .= '.php'; |
|
156 |
} |
|
157 |
if ((substr($file, 0, 1) == '/')) { |
|
158 |
return $file; |
|
159 |
} |
|
160 |
else |
|
161 |
{ |
|
162 |
return $this->path.'/'.$file; |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Displays escaped output. |
|
168 |
* |
|
169 |
* @param string $str String to escape |
|
170 |
* @return string Escaped string |
|
171 |
*/ |
|
172 |
public function e($str) { |
|
173 |
echo htmlentities($str); |
|
174 |
} |
|
175 |
} |