forked from Feuille2912/Wiki-Mind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_tag_helpers.php
218 lines (184 loc) · 6.13 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
<?php
/**
* Rails Style html tag helpers
*
* These are used by the other examples to make the code
* more concise and simpler to read.
*
* @copyright Copyright (c) Nicholas J Humfrey
* @license http://unlicense.org/
*/
/* 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) {
if ($key and $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 = '', $value = 'Submit', $options = array())
{
return input_tag('submit', $name, $value, $options);
}
function reset_tag($name = '', $value = 'Reset', $options = array())
{
return input_tag('reset', $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>";
}