/
html_tag_helpers.php
237 lines (202 loc) · 7.41 KB
/
html_tag_helpers.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Rails Style html tag helpers to make the EasyRdf examples simplier
*
* LICENSE
*
* Copyright (c) 2009-2010 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @copyright Copyright (c) 2009-2010 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
* @version $Id$
*/
/* Examples:
echo content_tag('p','Paragraph Tag', array('class'=>'foo'));
echo tag('br');
echo link_to('Hyperlink', 'http://www.example.com/?a=1&b=2');
echo tag('br');
echo form_tag();
echo label_tag('first_name').text_field_tag('first_name', 'Joe').tag('br');
echo label_tag('password').password_field_tag().tag('br');
echo label_tag('radio1_value1', 'Radio 1').radio_button_tag('radio1', 'value1').tag('br');
echo label_tag('radio1_value2', 'Radio 2').radio_button_tag('radio1', 'value2', true).tag('br');
echo label_tag('radio1_value3', 'Radio 3').radio_button_tag('radio1', 'value3').tag('br');
echo label_tag('check1', 'Check 1').check_box_tag('check1', 'value1').tag('br');
echo label_tag('check2', 'Check 2').check_box_tag('check2', 'value2', true).tag('br');
echo label_tag('check3', 'Check 3').check_box_tag('check3', 'value3').tag('br');
$options = array('Label 1' => 'value1', 'Label 2' => 'value2', 'Label 3' => 'value3');
echo label_tag('select1', 'Select Something:');
echo select_tag('select1', $options, 'value2').tag('br');
echo label_tag('textarea1', 'Type Something:');
echo text_area_tag('textarea1', "Hello World!").tag('br');
echo submit_tag();
echo form_end_tag();
*/
function tag_options($options)
{
$html = "";
foreach ($options as $key => $value) {
$html .= " ".htmlspecialchars($key)."=\"".
htmlspecialchars($value)."\"";
}
return $html;
}
function tag($name, $options=array(), $open=false)
{
return "<$name".tag_options($options).($open ? ">" : " />");
}
function content_tag($name, $content=null, $options=array())
{
return "<$name".tag_options($options).">".
htmlspecialchars($content)."</$name>";
}
function link_to($text, $uri=null, $options=array())
{
if ($uri == null) $uri = $text;
$options = array_merge(array('href' => $uri), $options);
return content_tag('a', $text, $options);
}
function link_to_self($text, $query_string, $options=array())
{
return link_to($text, $_SERVER['PHP_SELF'].'?'.$query_string, $options);
}
function image_tag($src, $options=array())
{
$options = array_merge(array('src' => $src), $options);
return tag('img', $options);
}
function input_tag($type, $name, $value=null, $options=array())
{
$options = array_merge(
array(
'type' => $type,
'name' => $name,
'id' => $name,
'value' => $value
),
$options
);
return tag('input', $options);
}
function text_field_tag($name, $default=null, $options=array())
{
$value = isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
return input_tag('text', $name, $value, $options);
}
function text_area_tag($name, $default=null, $options=array())
{
$content = isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
$options = array_merge(
array(
'name' => $name,
'id' => $name,
'cols' => 60,
'rows' => 5
),
$options
);
return content_tag('textarea', $content, $options);
}
function hidden_field_tag($name, $default=null, $options=array())
{
$value = isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
return input_tag('hidden', $name, $value, $options);
}
function password_field_tag($name='password', $default=null, $options=array())
{
$value = isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
return input_tag('password', $name, $value, $options);
}
function radio_button_tag($name, $value, $default=false, $options=array())
{
if ((isset($_REQUEST[$name]) and $_REQUEST[$name] == $value) or
(!isset($_REQUEST[$name]) and $default))
{
$options = array_merge(array('checked' => 'checked'), $options);
}
$options = array_merge(array('id' => $name.'_'.$value), $options);
return input_tag('radio', $name, $value, $options);
}
function check_box_tag($name, $value='1', $default=false, $options=array())
{
if ((isset($_REQUEST[$name]) and $_REQUEST[$name] == $value) or
(!isset($_REQUEST['submit']) and $default))
{
$options = array_merge(array('checked' => 'checked'),$options);
}
return input_tag('checkbox', $name, $value, $options);
}
function submit_tag($name='submit', $value='Submit', $options=array())
{
return input_tag('submit', $name, $value, $options);
}
function label_tag($name, $text=null, $options=array())
{
if ($text == null) {
$text = ucwords(str_replace('_', ' ', $name)).': ';
}
$options = array_merge(
array('for' => $name, 'id' => "label_for_$name"), $options
);
return content_tag('label', $text, $options);
}
function labeled_text_field_tag($name, $default=null, $options=array())
{
return label_tag($name).
text_field_tag($name, $default, $options);
}
function select_tag($name, $options, $default=null, $html_options=array())
{
$opts = '';
foreach ($options as $key => $value) {
$arr = array('value' => $value);
if ((isset($_REQUEST[$name]) and $_REQUEST[$name] == $value) or
(!isset($_REQUEST[$name]) and $default == $value))
{
$arr = array_merge(array('selected' => 'selected'),$arr);
}
$opts .= content_tag('option', $key, $arr);
}
$html_options = array_merge(array(
'name' => $name,
'id' => $name,
), $html_options);
return "<select".tag_options($html_options).">$opts</select>";
}
function form_tag($uri=null, $options=array())
{
if ($uri == null) {
$uri = $_SERVER['PHP_SELF'];
}
$options = array_merge(
array('method' => 'get', 'action' => $uri), $options
);
return tag('form', $options, true);
}
function form_end_tag()
{
return "</form>";
}