This repository has been archived by the owner on May 14, 2019. It is now read-only.
forked from bermi/akelos
/
ak_tag_helper.php
93 lines (84 loc) · 3.42 KB
/
ak_tag_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
# This file is part of the Akelos Framework
# (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
# See LICENSE and CREDITS for details
/**
* Use these methods to generate HTML tags programmatically when you can't use a Builder.
* By default, they output XHTML compliant tags.
*/
class AkTagHelper
{
/**
* Returns an empty HTML tag of type *name* which by default is XHTML
* compliant. Setting *open* to true will create an open tag compatible
* with HTML 4.0 and below. Add HTML attributes by passing an attributes
* array to *options*. For attributes with no value like (disabled and
* readonly), give it a value of true in the *options* array.
*
* Examples:
*
* <%= tag 'br' %>
* # => <br />
* <%= tag 'br', null, true %>
* # => <br>
* <%= tag 'input', { :type => 'text', :disabled => true } %>
* # => <input type="text" disabled="disabled" />
*/
static function tag($name, $options = null, $open = false) {
return '<'.$name.(!empty($options) ? AkTagHelper::tag_options($options) : '').($open ? '>' : ' />');
}
/**
* Returns an HTML block tag of type *name* surrounding the *content*. Add
* HTML attributes by passing an attributes array to *options*. For attributes
* with no value like (disabled and readonly), give it a value of true in
* the *options* array. You can use symbols or strings for the attribute names.
*
* <%= content_tag 'p', 'Hello world!' %>
* # => <p>Hello world!</p>
* <%= content_tag('div', content_tag('p', "Hello world!"), :class => "strong") %>
* # => <div class="strong"><p>Hello world!</p></div>
* <%= content_tag("select", options, :multiple => true) %>
* # => <select multiple="multiple">...options...</select>
*/
static function content_tag($name, $content, $options = null) {
return '<'.$name.(!empty($options) ? AkTagHelper::tag_options($options) : '').'>'.$content.'</'.$name.'>';
}
/**
* Returns a CDATA section for the given +content+. CDATA sections
* are used to escape blocks of text containing characters which would
* otherwise be recognized as markup. CDATA sections begin with the string
* <tt><![CDATA[</tt> and } with (and may not contain) the string
* <tt>]]></tt>.
*/
static function cdata_section($content) {
return '<![CDATA['.$content.']]>';
}
/**
* Returns the escaped +html+ without affecting existing escaped entities.
*
* <%= escape_once "1 > 2 & 3" %>
* # => "1 > 2 & 3"
*/
static function escape_once($html) {
return AkTagHelper::fix_double_escape(AkTextHelper::html_escape($html));
}
/**
* Fix double-escaped entities, such as &amp;, &#123;, etc.
*/
static function fix_double_escape($escaped) {
return preg_replace('/&([a-z]+|(#\d+));/i', '&$1;', $escaped);
}
static function tag_options($options) {
$formated_options = array();
foreach ($options as $key=>$value){
if(empty($value) && !is_string($value)){
continue;
}
if(!is_numeric($key) && !is_array($value) && !is_object($value)){
$formated_options[$key] = $key.'="'.AkTagHelper::escape_once($value).'"';
}
}
ksort($formated_options);
return empty($formated_options) ? '' : ' '.join(' ',$formated_options);
}
}