Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 206 lines (182 sloc) 5.911 kB
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
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
11 use lithium\util\String;
e7f3c31 @gwoo going lithium
gwoo authored
12
13a15b2 @alkemann adding doc block to Helper
alkemann authored
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
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
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
42 protected $_context = null;
43
c73c25e Adding docblocks.
psychic authored
44 /**
93112a6 @nateabele Adding `$_classes` declaration to `\template\Helper`.
nateabele authored
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
52 * Auto configuration properties.
53 *
54 * @var array
55 */
e7f3c31 @gwoo going lithium
gwoo authored
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
65 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize', 'async', 'autofocus'
e7f3c31 @gwoo going lithium
gwoo authored
66 );
67
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
68 public function __construct(array $config = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
69 $defaults = array('handlers' => array(), 'context' => null);
70 parent::__construct($config + $defaults);
71 }
72
73 /**
74 * Imports local string definitions into rendering context.
75 *
76 * @return void
77 */
78 protected function _init() {
79 parent::_init();
80
81 if (!$this->_context) {
82 return;
83 }
84 $this->_context->strings($this->_strings);
85
86 if ($this->_config['handlers']) {
87 $this->_context->handlers($this->_config['handlers']);
88 }
89 }
90
91 /**
92 * Escapes values according to the output type of the rendering context. Helpers that output to
93 * non-HTML/XML contexts should override this method accordingly.
94 *
95 * @param string $value
53a9fd9 @alkemann hooks fixes for lithium/template
alkemann authored
96 * @param mixed $method
97 * @param array $options
e7f3c31 @gwoo going lithium
gwoo authored
98 * @return mixed
99 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
100 public function escape($value, $method = null, array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
101 $defaults = array('escape' => true);
102 $options += $defaults;
103
104 if ($options['escape'] === false) {
105 return $value;
106 }
12632ed @nateabele Adding `view\Renderer::set()` to allow variables to be passed between…
nateabele authored
107 if (is_array($value)) {
108 return array_map(array($this, __FUNCTION__), $value);
109 }
e7f3c31 @gwoo going lithium
gwoo authored
110 return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
111 }
112
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored
113 /**
114 * Takes the defaults and current options, merges them and returns options which have
115 * the default keys removed and full set of options as the scope.
116 *
117 * @param array $defaults
118 * @param array $scope the complete set of options
119 * @return array $scope, $options
120 */
121 protected function _options(array $defaults, array $scope) {
122 $scope += $defaults;
123 $options = array_diff_key($scope, $defaults);
124 return array($scope, $options);
125 }
126
13a15b2 @alkemann adding doc block to Helper
alkemann authored
127 /**
128 * Render a string template after applying context filters
129 * Use examples in the Html::link() method:
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored
130 * `return $this->_render(__METHOD__, 'link', compact('title', 'url', 'options'), $scope);`
13a15b2 @alkemann adding doc block to Helper
alkemann authored
131 *
132 * @param string $method name of method that is calling the render (for context filters)
133 * @param string $string template key (in Helper::_strings) to render
a0930f4 @phishy fixed spelling errors
phishy authored
134 * @param array $params associated array of template inserts {:key} will be replaced by value
13a15b2 @alkemann adding doc block to Helper
alkemann authored
135 * @param array $options
136 * @return string Rendered HTML
137 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
138 protected function _render($method, $string, $params, array $options = array()) {
76771f0 @nateabele Adding test coverage to `\template\Helper`, refactoring to improve te…
nateabele authored
139 $strings = $this->_strings;
140
141 if ($this->_context) {
142 foreach ($params as $key => $value) {
ea227fa @davidpersson QA: Fixing lines where length > 100 chars.
davidpersson authored
143 $params[$key] = $this->_context->applyHandler(
144 $this, $method, $key, $value, $options
145 );
76771f0 @nateabele Adding test coverage to `\template\Helper`, refactoring to improve te…
nateabele authored
146 }
147 $strings = $this->_context->strings();
e7f3c31 @gwoo going lithium
gwoo authored
148 }
149 return String::insert(isset($strings[$string]) ? $strings[$string] : $string, $params);
150 }
151
13a15b2 @alkemann adding doc block to Helper
alkemann authored
152 /**
153 * Convert a set of options to HTML attributes
154 *
155 * @param array $params
156 * @param string $method
157 * @param array $options
158 * @return string
159 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
160 protected function _attributes($params, $method = null, array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
161 $defaults = array('escape' => true, 'prepend' => ' ', 'append' => '');
162 $options += $defaults;
163 $result = array();
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored
164
0a20c0b @nateabele Refactoring `Form` helper string templates to use wrapping instead of…
nateabele authored
165 if (!is_array($params)) {
166 return !$params ? '' : $options['prepend'] . $params;
167 }
e7f3c31 @gwoo going lithium
gwoo authored
168 foreach ($params as $key => $value) {
0a20c0b @nateabele Refactoring `Form` helper string templates to use wrapping instead of…
nateabele authored
169 if ($next = $this->_attribute($key, $value, $options)) {
170 $result[] = $next;
171 }
e7f3c31 @gwoo going lithium
gwoo authored
172 }
173 return $result ? $options['prepend'] . implode(' ', $result) . $options['append'] : '';
174 }
175
13a15b2 @alkemann adding doc block to Helper
alkemann authored
176 /**
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored
177 * Convert a key/value pair to a valid HTML attribute.
13a15b2 @alkemann adding doc block to Helper
alkemann authored
178 *
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored
179 * @param string $key The key name of the HTML attribute.
180 * @param mixed $value The HTML attribute value.
181 * @param array $options The options used when converting the key/value pair to attributes:
182 * - `'escape'` _boolean_: Indicates whether `$key` and `$value` should be
183 * HTML-escaped. Defaults to `true`.
184 * - `'format'` _string_: The format string. Defaults to `'%s="%s"'`.
185 * @return string Returns an HTML attribute/value pair, in the form of `'$key="$value"'`.
13a15b2 @alkemann adding doc block to Helper
alkemann authored
186 */
408737d @nateabele Cleaned up code formatting and added documentation in `\template\Help…
nateabele authored
187 protected function _attribute($key, $value, array $options = array()) {
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored
188 $defaults = array('escape' => true, 'format' => '%s="%s"');
e7f3c31 @gwoo going lithium
gwoo authored
189 $options += $defaults;
190
191 if (in_array($key, $this->_minimized)) {
ffe62c6 @nateabele Preventing `\template\Helper` from being too smart about attribute mi…
nateabele authored
192 $isMini = ($value == 1 || $value === true || $value == $key);
5faf140 @nateabele Refactoring `FormHelper` and adding test coverage.
nateabele authored
193 if (!($value = $isMini ? $key : $value)) {
194 return null;
195 }
e7f3c31 @gwoo going lithium
gwoo authored
196 }
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored
197 $value = (string) $value;
e7f3c31 @gwoo going lithium
gwoo authored
198
199 if ($options['escape']) {
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored
200 return sprintf($options['format'], $this->escape($key), $this->escape($value));
e7f3c31 @gwoo going lithium
gwoo authored
201 }
f8d75ca @gwoo adding more tests and refactoring `\template`. Most of the changes ar…
gwoo authored
202 return sprintf($options['format'], $key, $value);
e7f3c31 @gwoo going lithium
gwoo authored
203 }
204 }
205
206 ?>
Something went wrong with that request. Please try again.