Rev 29 | Rev 52 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 29 | Rev 51 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | <?php
|
1 | <?php
|
| 2 | 2 | ||
| - | 3 | namespace PointedEars\PHPX; |
|
| - | 4 | ||
| 3 | require_once __DIR__ . '/Application.php'; |
5 | require_once __DIR__ . '/Application.php'; |
| 4 | require_once __DIR__ . '/AbstractModel.php'; |
6 | require_once __DIR__ . '/AbstractModel.php'; |
| 5 | 7 | ||
| 6 | /**
|
8 | /**
|
| 7 | * A general view handled by a controller according to the MVC pattern
|
9 | * A general view handled by a controller according to the MVC pattern
|
| Line 14... | Line 16... | ||
| 14 | * Default template resource path
|
16 | * Default template resource path
|
| 15 | *
|
17 | *
|
| 16 | * @var string
|
18 | * @var string
|
| 17 | */
|
19 | */
|
| 18 | protected $_template = ''; |
20 | protected $_template = ''; |
| 19 | 21 | ||
| 20 | /**
|
22 | /**
|
| 21 | * Content that can be inserted in the template
|
23 | * Content that can be inserted in the template
|
| 22 | *
|
24 | *
|
| 23 | * @var string
|
25 | * @var string
|
| 24 | */
|
26 | */
|
| 25 | protected $_content = ''; |
27 | protected $_content = ''; |
| 26 | 28 | ||
| 27 | /**
|
29 | /**
|
| 28 | * Template variables. The variable name serves as item key, the item's value
|
30 | * Template variables. The variable name serves as item key, the item's value
|
| 29 | * is the variable value.
|
31 | * is the variable value.
|
| 30 | *
|
32 | *
|
| 31 | * @var array
|
33 | * @var array
|
| 32 | */
|
34 | */
|
| 33 | protected $_template_vars = array(); |
35 | protected $_template_vars = array(); |
| 34 | 36 | ||
| 35 | /**
|
37 | /**
|
| 36 | * Creates a new view
|
38 | * Creates a new view
|
| 37 | *
|
39 | *
|
| 38 | * @param string $template
|
40 | * @param string $template
|
| 39 | * Template resource path
|
41 | * Template resource path
|
| 40 | */
|
42 | */
|
| 41 | public function __construct($template) |
43 | public function __construct($template) |
| 42 | {
|
44 | {
|
| 43 | $this->_template = $template; |
45 | $this->_template = $template; |
| 44 | }
|
46 | }
|
| 45 | 47 | ||
| 46 | /**
|
48 | /**
|
| 47 | * Magic setter method used for defining template variables
|
49 | * Magic setter method used for defining template variables
|
| 48 | *
|
50 | *
|
| 49 | * @param string $name
|
51 | * @param string $name
|
| 50 | * Variable name
|
52 | * Variable name
|
| Line 64... | Line 66... | ||
| 64 | */
|
66 | */
|
| 65 | public function __get($name) |
67 | public function __get($name) |
| 66 | {
|
68 | {
|
| 67 | return $this->_template_vars[$name]; |
69 | return $this->_template_vars[$name]; |
| 68 | }
|
70 | }
|
| 69 | 71 | ||
| 70 | /**
|
72 | /**
|
| 71 | * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote),
|
73 | * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote),
|
| 72 | * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their
|
74 | * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their
|
| 73 | * HTML character entity references, if any, or their numeric HTML character
|
75 | * HTML character entity references, if any, or their numeric HTML character
|
| 74 | * reference (as required primarily in HTML for attribute values and element
|
76 | * reference (as required primarily in HTML for attribute values and element
|
| Line 89... | Line 91... | ||
| 89 | foreach ($value->getPropertyVars() as $varName) |
91 | foreach ($value->getPropertyVars() as $varName) |
| 90 | {
|
92 | {
|
| 91 | $value->$varName = self::escape($value->$varName); |
93 | $value->$varName = self::escape($value->$varName); |
| 92 | }
|
94 | }
|
| 93 | }
|
95 | }
|
| 94 | 96 | ||
| 95 | return $value; |
97 | return $value; |
| 96 | }
|
98 | }
|
| 97 | else
|
99 | else
|
| 98 | {
|
100 | {
|
| 99 | if (is_string($value)) |
101 | if (is_string($value)) |
| Line 103... | Line 105... | ||
| 103 | {
|
105 | {
|
| 104 | $encoding = 'ISO-8859-1'; |
106 | $encoding = 'ISO-8859-1'; |
| 105 | }
|
107 | }
|
| 106 | return htmlspecialchars($value, ENT_QUOTES, $encoding); |
108 | return htmlspecialchars($value, ENT_QUOTES, $encoding); |
| 107 | }
|
109 | }
|
| 108 | 110 | ||
| 109 | return $value; |
111 | return $value; |
| 110 | }
|
112 | }
|
| 111 | }
|
113 | }
|
| 112 | 114 | ||
| 113 | /**
|
115 | /**
|
| 114 | * Assigns a value to a template variable
|
116 | * Assigns a value to a template variable
|
| 115 | *
|
117 | *
|
| 116 | * @param string $name
|
118 | * @param string $name
|
| 117 | * Variable name
|
119 | * Variable name
|
| Line 132... | Line 134... | ||
| 132 | }
|
134 | }
|
| 133 | 135 | ||
| 134 | $this->$name = $value; |
136 | $this->$name = $value; |
| 135 | return $value; |
137 | return $value; |
| 136 | }
|
138 | }
|
| 137 | 139 | ||
| 138 | /**
|
140 | /**
|
| 139 | * Renders the view by including a template
|
141 | * Renders the view by including a template
|
| 140 | *
|
142 | *
|
| 141 | * @param string $template
|
143 | * @param string $template
|
| 142 | * Optional alternative template resource path.
|
144 | * Optional alternative template resource path.
|
| 143 | * If not provided, the default template ($template property) will be used.
|
145 | * If not provided, the default template ($template property) will be used.
|
| 144 | * @throws Exception if no template has been defined before
|
146 | * @throws Exception if no template has been defined before
|
| 145 | */
|
147 | */
|
| 146 | public function render($template = null, $content = null) |
148 | public function render($template = null, $content = null) |
| 147 | {
|
149 | {
|
| 148 | if (!is_null($content)) |
150 | if (!is_null($content)) |
| 149 | {
|
151 | {
|
| 150 | ob_start(); |
152 | ob_start(); |
| 151 | require_once $content; |
153 | require_once $content; |
| 152 | $this->_content = ob_get_contents(); |
154 | $this->_content = ob_get_contents(); |
| 153 | ob_end_clean(); |
155 | ob_end_clean(); |
| 154 | }
|
156 | }
|
| 155 | 157 | ||
| 156 | if (!is_null($template)) |
158 | if (!is_null($template)) |
| 157 | {
|
159 | {
|
| 158 | require $template; |
160 | require $template; |
| 159 | }
|
161 | }
|
| 160 | elseif ($this->_template) |
162 | elseif ($this->_template) |
| Line 164... | Line 166... | ||
| 164 | else
|
166 | else
|
| 165 | {
|
167 | {
|
| 166 | throw new Exception('No template defined'); |
168 | throw new Exception('No template defined'); |
| 167 | }
|
169 | }
|
| 168 | }
|
170 | }
|
| 169 | 171 | ||
| 170 | /**
|
172 | /**
|
| 171 | * Returns the content for insertion into the template
|
173 | * Returns the content for insertion into the template
|
| 172 | */
|
174 | */
|
| 173 | public function getContent() |
175 | public function getContent() |
| 174 | {
|
176 | {
|
| 175 | return $this->_content; |
177 | return $this->_content; |
| 176 | }
|
178 | }
|
| 177 | 179 | ||
| 178 | /**
|
180 | /**
|
| 179 | * @param string[optional] $controller
|
181 | * @param string[optional] $controller
|
| 180 | * @param string[optional] $action
|
182 | * @param string[optional] $action
|
| 181 | * @param int[optional] $id
|
183 | * @param int[optional] $id
|
| 182 | * @see Application::getURL()
|
184 | * @see Application::getURL()
|