Rearranging scripts to reduce the hassle of updating local application whenever scrip...
[akelos.git] / lib / AkActionView / helpers / tag_helper.php
blobd3b1260ba9eb2fffaf7a7c038894d1154925c382
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActionView
13 * @subpackage Helpers
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 /**
21 * Use these methods to generate HTML tags programmatically when you can't use a Builder.
22 * By default, they output XHTML compliant tags.
24 class TagHelper
26 /**
27 * Returns an empty HTML tag of type *name* which by default is XHTML
28 * compliant. Setting *open* to true will create an open tag compatible
29 * with HTML 4.0 and below. Add HTML attributes by passing an attributes
30 * array to *options*. For attributes with no value like (disabled and
31 * readonly), give it a value of true in the *options* array.
33 * Examples:
35 * <%= tag 'br' %>
36 * # => <br />
37 * <%= tag 'br', null, true %>
38 * # => <br>
39 * <%= tag 'input', { :type => 'text', :disabled => true } %>
40 * # => <input type="text" disabled="disabled" />
42 function tag($name, $options = null, $open = false)
44 return '<'.$name.(!empty($options) ? TagHelper::_tag_options($options) : '').($open ? '>' : ' />');
47 /**
48 * Returns an HTML block tag of type *name* surrounding the *content*. Add
49 * HTML attributes by passing an attributes array to *options*. For attributes
50 * with no value like (disabled and readonly), give it a value of true in
51 * the *options* array. You can use symbols or strings for the attribute names.
53 * <%= content_tag 'p', 'Hello world!' %>
54 * # => <p>Hello world!</p>
55 * <%= content_tag('div', content_tag('p', "Hello world!"), :class => "strong") %>
56 * # => <div class="strong"><p>Hello world!</p></div>
57 * <%= content_tag("select", options, :multiple => true) %>
58 * # => <select multiple="multiple">...options...</select>
60 function content_tag($name, $content, $options = null)
62 return '<'.$name.(!empty($options) ? TagHelper::_tag_options($options) : '').'>'.$content.'</'.$name.'>';
65 /**
66 * Returns a CDATA section for the given +content+. CDATA sections
67 * are used to escape blocks of text containing characters which would
68 * otherwise be recognized as markup. CDATA sections begin with the string
69 * <tt>&lt;![CDATA[</tt> and } with (and may not contain) the string
70 * <tt>]]></tt>.
72 function cdata_section($content)
74 return '<![CDATA['.$content.']]>';
78 /**
79 * Returns the escaped +html+ without affecting existing escaped entities.
81 * <%= escape_once "1 > 2 &amp; 3" %>
82 * # => "1 &gt; 2 &amp; 3"
84 function escape_once($html)
86 static $charset;
87 if(empty($charset)){
88 $charset = Ak::locale('charset');
90 return TagHelper::_fix_double_escape(htmlentities($html, ENT_COMPAT, $charset));
93 /**
94 * Fix double-escaped entities, such as &amp;amp;, &amp;#123;, etc.
96 function _fix_double_escape($escaped)
98 return preg_replace('/&amp;([a-z]+|(#\d+));/i', '&$1;', $escaped);
101 function _tag_options($options)
103 $formated_options = array();
104 foreach ($options as $key=>$value){
105 if(empty($value) && !is_string($value)){
106 continue;
108 if(!is_numeric($key) && !is_array($value) && !is_object($value)){
109 $formated_options[$key] = $key.'="'.TagHelper::escape_once($value).'"';
112 ksort($formated_options);
113 return empty($formated_options) ? '' : ' '.join(' ',$formated_options);