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