Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
51 | PointedEar | 3 | namespace PointedEars\PHPX; |
4 | |||
27 | PointedEar | 5 | /** |
6 | * A general view handled by a controller according to the MVC pattern |
||
7 | * |
||
64 | PointedEar | 8 | * @author Thomas 'PointedEars' Lahn <php@PointedEars.de> |
27 | PointedEar | 9 | */ |
10 | class View |
||
11 | { |
||
12 | /** |
||
13 | * Default template resource path |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $_template = ''; |
||
51 | PointedEar | 18 | |
27 | PointedEar | 19 | /** |
20 | * Content that can be inserted in the template |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $_content = ''; |
||
51 | PointedEar | 25 | |
27 | PointedEar | 26 | /** |
27 | * Template variables. The variable name serves as item key, the item's value |
||
28 | * is the variable value. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $_template_vars = array(); |
||
51 | PointedEar | 33 | |
27 | PointedEar | 34 | /** |
35 | * Creates a new view |
||
36 | * |
||
37 | * @param string $template |
||
38 | * Template resource path |
||
39 | */ |
||
40 | public function __construct($template) |
||
41 | { |
||
42 | $this->_template = $template; |
||
43 | } |
||
51 | PointedEar | 44 | |
27 | PointedEar | 45 | /** |
46 | * Magic setter method used for defining template variables |
||
47 | * |
||
48 | * @param string $name |
||
49 | * Variable name |
||
50 | * @param mixed $value |
||
51 | * Variable value |
||
52 | */ |
||
53 | public function __set($name, $value) |
||
54 | { |
||
55 | $this->_template_vars[$name] = $value; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Magic getter method used for retrieving values of template variables |
||
60 | * |
||
61 | * @param string $name |
||
62 | * Variable name |
||
63 | */ |
||
64 | public function __get($name) |
||
65 | { |
||
66 | return $this->_template_vars[$name]; |
||
67 | } |
||
51 | PointedEar | 68 | |
27 | PointedEar | 69 | /** |
70 | * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote), |
||
71 | * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their |
||
72 | * HTML character entity references, if any, or their numeric HTML character |
||
73 | * reference (as required primarily in HTML for attribute values and element |
||
74 | * content). |
||
75 | * |
||
76 | * @param mixed $value |
||
77 | */ |
||
78 | public function escape($value) |
||
79 | { |
||
80 | if (is_array($value)) |
||
81 | { |
||
82 | return array_map(array('self', 'escape'), $value); |
||
83 | } |
||
84 | else if (is_object($value)) |
||
85 | { |
||
86 | if ($value instanceof AbstractModel) |
||
87 | { |
||
88 | foreach ($value->getPropertyVars() as $varName) |
||
89 | { |
||
90 | $value->$varName = self::escape($value->$varName); |
||
91 | } |
||
92 | } |
||
51 | PointedEar | 93 | |
27 | PointedEar | 94 | return $value; |
95 | } |
||
96 | else |
||
97 | { |
||
98 | if (is_string($value)) |
||
99 | { |
||
100 | $encoding = mb_detect_encoding($value); |
||
101 | if ($encoding === 'ASCII') |
||
102 | { |
||
103 | $encoding = 'ISO-8859-1'; |
||
104 | } |
||
105 | return htmlspecialchars($value, ENT_QUOTES, $encoding); |
||
106 | } |
||
51 | PointedEar | 107 | |
27 | PointedEar | 108 | return $value; |
109 | } |
||
110 | } |
||
51 | PointedEar | 111 | |
27 | PointedEar | 112 | /** |
113 | * Assigns a value to a template variable |
||
114 | * |
||
115 | * @param string $name |
||
116 | * Variable name |
||
117 | * @param mixed $value |
||
118 | * Variable value |
||
119 | * @param bool $escape |
||
120 | * If <code>true</code>, replace all potentially conflicting characters |
||
121 | * in <var>$value</var> with their HTML entity references. The default is |
||
122 | * <code>false</code>. |
||
123 | * @return mixed The assigned value (after possible HTML encoding) |
||
124 | * @see View::escape() |
||
125 | */ |
||
126 | public function assign($name, $value, $escape = false) |
||
127 | { |
||
128 | if ($escape) |
||
129 | { |
||
130 | $value = $this->escape($value); |
||
131 | } |
||
132 | |||
133 | $this->$name = $value; |
||
134 | return $value; |
||
135 | } |
||
51 | PointedEar | 136 | |
27 | PointedEar | 137 | /** |
138 | * Renders the view by including a template |
||
139 | * |
||
140 | * @param string $template |
||
141 | * Optional alternative template resource path. |
||
142 | * If not provided, the default template ($template property) will be used. |
||
143 | * @throws Exception if no template has been defined before |
||
144 | */ |
||
145 | public function render($template = null, $content = null) |
||
146 | { |
||
51 | PointedEar | 147 | if (!is_null($content)) |
27 | PointedEar | 148 | { |
149 | ob_start(); |
||
150 | require_once $content; |
||
151 | $this->_content = ob_get_contents(); |
||
152 | ob_end_clean(); |
||
153 | } |
||
51 | PointedEar | 154 | |
27 | PointedEar | 155 | if (!is_null($template)) |
156 | { |
||
157 | require $template; |
||
158 | } |
||
159 | elseif ($this->_template) |
||
160 | { |
||
161 | require $this->_template; |
||
162 | } |
||
163 | else |
||
164 | { |
||
64 | PointedEar | 165 | throw new \Exception('No template defined'); |
27 | PointedEar | 166 | } |
167 | } |
||
51 | PointedEar | 168 | |
27 | PointedEar | 169 | /** |
170 | * Returns the content for insertion into the template |
||
171 | */ |
||
172 | public function getContent() |
||
173 | { |
||
174 | return $this->_content; |
||
175 | } |
||
51 | PointedEar | 176 | |
27 | PointedEar | 177 | /** |
178 | * @param string[optional] $controller |
||
179 | * @param string[optional] $action |
||
180 | * @param int[optional] $id |
||
181 | * @see Application::getURL() |
||
182 | */ |
||
183 | public function getURL($controller = null, $action = null, $id = null) |
||
184 | { |
||
185 | return Application::getInstance()->getURL($controller, $action, $id); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | ?> |