diff options
author | ewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-20 06:04:43 +0000 |
---|---|---|
committer | ewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-20 06:04:43 +0000 |
commit | a8833f0ab81df477c8a19b221a9ae8a90294bdf8 (patch) | |
tree | 1a38a47a466a565d38df9b549ad727bd5f363b19 /engine | |
parent | 2da8c3e8428f09bab7aefa39cadc0a4f2d6f7a80 (diff) | |
download | elgg-a8833f0ab81df477c8a19b221a9ae8a90294bdf8.tar.gz elgg-a8833f0ab81df477c8a19b221a9ae8a90294bdf8.tar.bz2 |
Refs #2143: Added elgg_format_attributes() for generating an attribute string from an associative array. DRYed up input/output url
git-svn-id: http://code.elgg.org/elgg/trunk@7354 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine')
-rw-r--r-- | engine/lib/output.php | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/engine/lib/output.php b/engine/lib/output.php index dd5c4cf59..61db401a1 100644 --- a/engine/lib/output.php +++ b/engine/lib/output.php @@ -140,6 +140,85 @@ function elgg_format_url($url) { } /** + * Converts an associative array into a string of well-formed attributes + * + * @note usually for HTML, but could be useful for XML too... + * + * @param array $attrs An associative array of attr => val pairs + * + * @return string HTML attributes to be inserted into a tag (e.g., <tag $attrs>) + */ +function elgg_format_attributes(array $attrs = array()) { + $attrs = elgg_clean_vars($attrs); + $attributes = array(); + + if (isset($attrs['js'])) { + //@todo deprecated notice? + + if (!empty($attrs['js'])) { + $attributes[] = $attrs['js']; + } + + unset($attrs['js']); + } + + foreach ($attrs as $attr => $val) { + $attr = strtolower($attr); + + if ($val === TRUE) { + $val = $attr; //e.g. checked => TRUE ==> checked="checked" + } + + // ignore $vars['entity'] => ElggEntity stuff + if (is_not_null($val) && (is_array($val) || is_string($val))) { + + // allow $vars['class'] => array('one', 'two'); + // @todo what about $vars['style']? Needs to be semi-colon separated... + if (is_array($val)) { + $val = implode(' ', $val); + } + + $val = htmlspecialchars($val); + $attributes[] = "$attr=\"$val\""; + } + } + + return implode(' ', $attributes); +} + + +/** + * Preps an associative array for use in {@link elgg_format_attributes()}. + * + * Removes all the junk that {@link elgg_view()} puts into $vars. + * Maintains backward compatibility with attributes like 'internalname' and 'internalid' + * + * @note This function is called automatically by elgg_format_attributes(). No need to + * call it yourself before using elgg_format_attributes(). + * + * @param array $vars The raw $vars array with all it's dirtiness (config, url, etc.) + * + * @return array The array, ready to be used in elgg_format_attributes(). + */ +function elgg_clean_vars(array $vars = array()) { + unset($vars['config']); + unset($vars['url']); + + // backwards compatibility code + if (isset($vars['internalname'])) { + $vars['name'] = $vars['internalname']; + unset($vars['internalname']); + } + + if (isset($vars['internalid'])) { + $vars['id'] = $vars['internalid']; + unset($vars['internalid']); + } + + return $vars; +} + +/** * Converts shorthand urls to absolute urls. * * If the url is already absolute or protocol-relative, no change is made. @@ -164,7 +243,7 @@ function elgg_normalize_url($url) { elseif (preg_match("#^[^/]*\.php(\?.*)?$#i", $url)) { return elgg_get_site_url().$url; } - + // 'example.com', 'example.com/subpage' elseif (preg_match("#^[^/]*\.#i", $url)) { return "http://$url"; @@ -311,6 +390,8 @@ function elgg_strip_tags($string) { return $string; } + + /** * Filters a string into an array of significant words * |