Skip to content
Newer
Older
100644 226 lines (201 sloc) 6.82 KB
e7f3c31 @gwoo going lithium
gwoo authored Oct 12, 2009
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
f99a12f @mehlah Happy 2013!
mehlah authored Jan 1, 2013
5 * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9 namespace lithium\template;
10
6fb3f64 @daschl change _use lithium_ commands and remove the prefixed slash
daschl authored Nov 16, 2010
11 use lithium\util\String;
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
12
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
13 /**
14 * Abstract class for template helpers to extend.
15 * Supplies the basic functionality of _render and _options,
16 * as well as escaping.
17 *
18 */
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
19 abstract class Helper extends \lithium\core\Object {
20
21 /**
22 * Maps helper method names to content types as defined by the `Media` class, where key are
23 * method names, and values are the content type that the method name outputs a link to.
24 *
25 * @var array
26 */
27 public $contentMap = array();
28
29 /**
30 * Holds string templates which will be merged into the rendering context.
31 *
32 * @var array
33 */
34 protected $_strings = array();
35
c73c25e Adding docblocks.
psychic authored Apr 27, 2010
36 /**
37 * The Renderer object this Helper is bound to.
38 *
39 * @var lithium\template\view\Renderer
40 * @see lithium\template\view\Renderer
41 */
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
42 protected $_context = null;
43
c73c25e Adding docblocks.
psychic authored Apr 27, 2010
44 /**
93112a6 @nateabele Adding `$_classes` declaration to `\template\Helper`.
nateabele authored Mar 4, 2011
45 * This property can be overwritten with any class dependencies a helper subclass has.
46 *
47 * @var array
48 */
49 protected $_classes = array();
50
51 /**
c73c25e Adding docblocks.
psychic authored Apr 27, 2010
52 * Auto configuration properties.
53 *
54 * @var array
55 */
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
56 protected $_autoConfig = array('classes' => 'merge', 'context');
57
58 /**
59 * List of minimized HTML attributes.
60 *
61 * @var array
62 */
63 protected $_minimized = array(
64 'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected', 'defer', 'ismap',
95c2a3c @nateabele Adding `'autofocus'` to list of minimized HTML attributes.
nateabele authored Jul 24, 2011
65 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize', 'async', 'autofocus'
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
66 );
67
9d36d86 @blainesch Complete docs in the `template` folder.
blainesch authored May 18, 2013
68 /**
69 * Sets up defaults and passes to parent to setup class.
70 *
71 * @param array $config Configuration options.
72 * @return void
73 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored Feb 16, 2010
74 public function __construct(array $config = array()) {
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
75 $defaults = array('handlers' => array(), 'context' => null);
76 parent::__construct($config + $defaults);
77 }
78
79 /**
80 * Imports local string definitions into rendering context.
81 *
82 * @return void
83 */
84 protected function _init() {
85 parent::_init();
86
87 if (!$this->_context) {
88 return;
89 }
90 $this->_context->strings($this->_strings);
91
92 if ($this->_config['handlers']) {
93 $this->_context->handlers($this->_config['handlers']);
94 }
95 }
96
97 /**
98 * Escapes values according to the output type of the rendering context. Helpers that output to
99 * non-HTML/XML contexts should override this method accordingly.
100 *
101 * @param string $value
53a9fd9 @alkemann hooks fixes for lithium/template
alkemann authored Dec 10, 2009
102 * @param mixed $method
103 * @param array $options
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
104 * @return mixed
105 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored Feb 17, 2010
106 public function escape($value, $method = null, array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
107 $defaults = array('escape' => true);
108 $options += $defaults;
109
110 if ($options['escape'] === false) {
111 return $value;
112 }
12632ed @nateabele Adding `view\Renderer::set()` to allow variables to be passed between…
nateabele authored Feb 5, 2010
113 if (is_array($value)) {
114 return array_map(array($this, __FUNCTION__), $value);
115 }
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
116 return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
117 }
118
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored Mar 8, 2010
119 /**
120 * Takes the defaults and current options, merges them and returns options which have
121 * the default keys removed and full set of options as the scope.
122 *
123 * @param array $defaults
124 * @param array $scope the complete set of options
125 * @return array $scope, $options
126 */
127 protected function _options(array $defaults, array $scope) {
128 $scope += $defaults;
129 $options = array_diff_key($scope, $defaults);
130 return array($scope, $options);
131 }
132
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
133 /**
134 * Render a string template after applying context filters
135 * Use examples in the Html::link() method:
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored Oct 10, 2010
136 * `return $this->_render(__METHOD__, 'link', compact('title', 'url', 'options'), $scope);`
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
137 *
138 * @param string $method name of method that is calling the render (for context filters)
139 * @param string $string template key (in Helper::_strings) to render
a0930f4 @phishy fixed spelling errors
phishy authored Jun 11, 2011
140 * @param array $params associated array of template inserts {:key} will be replaced by value
cca5970 @jails Template\Helper: the `'handlers'` option of the `_render()` method is…
jails authored Nov 23, 2012
141 * @param array $options Available options:
142 * - `'handlers'` _array_: Before inserting `$params` inside the string template,
143 * `$this->_context`'s handlers are applied to each value of `$params` according
144 * to the key (e.g `$params['url']`, which is processed by the `'url'` handler
145 * via `$this->_context->applyHandler()`).
146 * The `'handlers'` option allow to set custom mapping beetween `$params`'s key and
147 * `$this->_context`'s handlers. e.g. the following handler:
148 * `'handlers' => array('url' => 'path')` will make `$params['url']` to be
149 * processed by the `'path'` handler instead of the `'url'` one.
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
150 * @return string Rendered HTML
151 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored Feb 17, 2010
152 protected function _render($method, $string, $params, array $options = array()) {
76771f0 @nateabele Adding test coverage to `\template\Helper`, refactoring to improve te…
nateabele authored Oct 30, 2010
153 $strings = $this->_strings;
154
67c450c @jails Add scope to `lithium\net\http\Router` and `lithium\net\http\Media`.
jails authored Feb 16, 2013
155 if (isset($params['options']['scope'])) {
156 $options['scope'] = $params['options']['scope'];
157 unset($params['options']['scope']);
158 }
159
76771f0 @nateabele Adding test coverage to `\template\Helper`, refactoring to improve te…
nateabele authored Oct 31, 2010
160 if ($this->_context) {
161 foreach ($params as $key => $value) {
cca5970 @jails Template\Helper: the `'handlers'` option of the `_render()` method is…
jails authored Nov 23, 2012
162 $handler = isset($options['handlers'][$key]) ? $options['handlers'][$key] : $key;
ea227fa @davidpersson QA: Fixing lines where length > 100 chars.
davidpersson authored Jan 30, 2011
163 $params[$key] = $this->_context->applyHandler(
cca5970 @jails Template\Helper: the `'handlers'` option of the `_render()` method is…
jails authored Nov 23, 2012
164 $this, $method, $handler, $value, $options
ea227fa @davidpersson QA: Fixing lines where length > 100 chars.
davidpersson authored Jan 30, 2011
165 );
76771f0 @nateabele Adding test coverage to `\template\Helper`, refactoring to improve te…
nateabele authored Oct 31, 2010
166 }
167 $strings = $this->_context->strings();
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
168 }
169 return String::insert(isset($strings[$string]) ? $strings[$string] : $string, $params);
170 }
171
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
172 /**
173 * Convert a set of options to HTML attributes
174 *
175 * @param array $params
176 * @param string $method
177 * @param array $options
178 * @return string
179 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored Feb 17, 2010
180 protected function _attributes($params, $method = null, array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
181 $defaults = array('escape' => true, 'prepend' => ' ', 'append' => '');
182 $options += $defaults;
183 $result = array();
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored Oct 10, 2010
184
0a20c0b @nateabele Refactoring `Form` helper string templates to use wrapping instead of…
nateabele authored Jul 28, 2011
185 if (!is_array($params)) {
186 return !$params ? '' : $options['prepend'] . $params;
187 }
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
188 foreach ($params as $key => $value) {
0a20c0b @nateabele Refactoring `Form` helper string templates to use wrapping instead of…
nateabele authored Jul 28, 2011
189 if ($next = $this->_attribute($key, $value, $options)) {
190 $result[] = $next;
191 }
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
192 }
193 return $result ? $options['prepend'] . implode(' ', $result) . $options['append'] : '';
194 }
195
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
196 /**
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored Oct 10, 2010
197 * Convert a key/value pair to a valid HTML attribute.
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
198 *
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored Oct 10, 2010
199 * @param string $key The key name of the HTML attribute.
200 * @param mixed $value The HTML attribute value.
201 * @param array $options The options used when converting the key/value pair to attributes:
202 * - `'escape'` _boolean_: Indicates whether `$key` and `$value` should be
203 * HTML-escaped. Defaults to `true`.
204 * - `'format'` _string_: The format string. Defaults to `'%s="%s"'`.
205 * @return string Returns an HTML attribute/value pair, in the form of `'$key="$value"'`.
13a15b2 @alkemann adding doc block to Helper
alkemann authored May 26, 2010
206 */
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored Oct 10, 2010
207 protected function _attribute($key, $value, array $options = array()) {
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored Mar 8, 2010
208 $defaults = array('escape' => true, 'format' => '%s="%s"');
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
209 $options += $defaults;
210
211 if (in_array($key, $this->_minimized)) {
fa4fe6b @nateabele Cleaning up coding standards warnings
nateabele authored Apr 17, 2013
212 $isMini = ($value === 1 || $value === true || $value === $key);
5faf140 @nateabele Refactoring `FormHelper` and adding test coverage.
nateabele authored Oct 19, 2009
213 if (!($value = $isMini ? $key : $value)) {
214 return null;
215 }
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
216 }
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored Mar 8, 2010
217 $value = (string) $value;
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
218
219 if ($options['escape']) {
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored Mar 8, 2010
220 return sprintf($options['format'], $this->escape($key), $this->escape($value));
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
221 }
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored Mar 8, 2010
222 return sprintf($options['format'], $key, $value);
e7f3c31 @gwoo going lithium
gwoo authored Oct 13, 2009
223 }
224 }
225
226 ?>
Something went wrong with that request. Please try again.