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