126 |
- |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
A class that writes html either into a local buffer or directly to the output. Freely distributable as long as this header is kept at the top of the file and not modified.
|
|
|
5 |
|
|
|
6 |
@author Jean-Georges Estiot <jgestiot@gmail.com>
|
|
|
7 |
@version 2.0
|
|
|
8 |
|
|
|
9 |
*/
|
|
|
10 |
class html
|
|
|
11 |
{
|
|
|
12 |
static private $spaces_per_indent;
|
|
|
13 |
static private $stack;
|
|
|
14 |
static private $attributes;
|
|
|
15 |
static private $indent_level;
|
|
|
16 |
static private $buffer;
|
|
|
17 |
static private $buffered;
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
init() must be called once before the class is used by the application. It sets up the indent, defaults to buffered output and calls reset().
|
|
|
22 |
|
|
|
23 |
@param int $spaces The number of spaces to use in the indentation
|
|
|
24 |
|
|
|
25 |
@return void does not return anything
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
static public function init($spaces = 2)
|
|
|
29 |
{
|
|
|
30 |
self::$spaces_per_indent = $spaces;
|
|
|
31 |
self::$buffered = true;
|
|
|
32 |
self::reset();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
Resets some of the key elements of the class: the stack, the indent, the attributes and the buffer.
|
|
|
39 |
|
|
|
40 |
@return void does not return anything
|
|
|
41 |
*/
|
|
|
42 |
|
|
|
43 |
static public function reset()
|
|
|
44 |
{
|
|
|
45 |
self::$stack = array();
|
|
|
46 |
self::$indent_level = 0;
|
|
|
47 |
self::clear_attributes();
|
|
|
48 |
self::clear_buffer();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Returns the content of the buffer
|
|
|
55 |
|
|
|
56 |
@return string The content of the buffer.
|
|
|
57 |
*/
|
|
|
58 |
|
|
|
59 |
static public function get_buffer()
|
|
|
60 |
{
|
|
|
61 |
$temp = self::$buffer;
|
|
|
62 |
self::clear_buffer();
|
|
|
63 |
return $temp;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
*clears the buffer
|
|
|
70 |
|
|
|
71 |
@return void does not return anything
|
|
|
72 |
*/
|
|
|
73 |
static private function clear_buffer()
|
|
|
74 |
{
|
|
|
75 |
self::$buffer = '';
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
Sets the buffering to true or false. If false, the class will output the tags immediately. If true, it will keep all content into the buffer for later use.
|
|
|
82 |
|
|
|
83 |
@param bool $how True for buffered and false for unbuffered.
|
|
|
84 |
|
|
|
85 |
@return void does not return anything
|
|
|
86 |
*/
|
|
|
87 |
|
|
|
88 |
static public function set_buffered($how)
|
|
|
89 |
{
|
|
|
90 |
self::$buffered = $how;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
Writes a string to the buffer. This is the only function in the class that outputs. If $indent is set to true, the string output is correctly indented, relative to other code.
|
|
|
97 |
|
|
|
98 |
@param string $string The string to write to output
|
|
|
99 |
@param bool $indent True to indent the string or false otherwise
|
|
|
100 |
|
|
|
101 |
@return void does not return anything
|
|
|
102 |
*/
|
|
|
103 |
|
|
|
104 |
static public function write($string, $indent = false)
|
|
|
105 |
{
|
|
|
106 |
|
|
|
107 |
if ($indent) $string = self::make_indent() . $string;
|
|
|
108 |
if (self::$buffered) self::$buffer.= $string;
|
|
|
109 |
else
|
|
|
110 |
{
|
|
|
111 |
echo $string;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
Adds a single attribute to the next tag. Example for an `<input>` tag: add_attribute('type','checkbox');
|
|
|
119 |
|
|
|
120 |
@param string $name The name of the attribute
|
|
|
121 |
@param string $value The value of the attribute
|
|
|
122 |
|
|
|
123 |
@return void does not return anything
|
|
|
124 |
*/
|
|
|
125 |
|
|
|
126 |
static public function add_attribute($name, $value)
|
|
|
127 |
{
|
|
|
128 |
|
|
|
129 |
self::$attributes[$name] = $value;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
Adds more than one attribute to the next tag
|
|
|
136 |
The attributes for the tag are passed in the format array('attribute'=>'value'). So array('id'=>'tag_id','class'=>'myclass') would produce this div tag:
|
|
|
137 |
`<div id=tag_id" class="myclass">`
|
|
|
138 |
|
|
|
139 |
@param array $ar The array holding the attributes
|
|
|
140 |
|
|
|
141 |
@return void does not return anything
|
|
|
142 |
*/
|
|
|
143 |
|
|
|
144 |
static public function add_attributes($ar)
|
|
|
145 |
{
|
|
|
146 |
|
|
|
147 |
if (is_array($ar))
|
|
|
148 |
{
|
|
|
149 |
|
|
|
150 |
self::$attributes = array_merge(self::$attributes, $ar);
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
Outputs the attributes for the tag that were added with add_attribute() or add_attributes().
|
|
|
158 |
|
|
|
159 |
Example: if the previously-added attributes are array('id'=>'tag_id','class'=>'myclass'), expand_attributes() will
|
|
|
160 |
produce the string 'id=tag_id" class="myclass'.
|
|
|
161 |
Important: all attributes are cleared once the string is output.
|
|
|
162 |
|
|
|
163 |
@return void does not return anything
|
|
|
164 |
*/
|
|
|
165 |
|
|
|
166 |
static private function expand_attributes()
|
|
|
167 |
{
|
|
|
168 |
|
|
|
169 |
foreach (self::$attributes as $idx => $val)
|
|
|
170 |
{
|
|
|
171 |
self::write(' ' . $idx . '=' . '"' . $val . '"');
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
self::clear_attributes(); //always clear attributes when finished
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
Clears all attributes. (The next tag will have no attributes)
|
|
|
183 |
|
|
|
184 |
@return void does not return anything
|
|
|
185 |
*/
|
|
|
186 |
|
|
|
187 |
static public function clear_attributes()
|
|
|
188 |
{
|
|
|
189 |
|
|
|
190 |
self::$attributes = array();
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
Generates a tag. There are three flavours:
|
|
|
197 |
|
|
|
198 |
1) Open tags with no close and no content. Just the attributes if required: `<div class="myclass">`. This tag will require a call to close()
|
|
|
199 |
|
|
|
200 |
2) Tags that never require a close such as `<img href="image.jpg">`
|
|
|
201 |
|
|
|
202 |
3) Tags that have content and are immediately terminated on the same line `<title>Hello</title>`
|
|
|
203 |
|
|
|
204 |
For the first, use tag('div');
|
|
|
205 |
|
|
|
206 |
For the second, use tag('img',false,false) or the function single_tag()
|
|
|
207 |
|
|
|
208 |
For the third, simply pass the tag's content like so: tag('title','Hello')
|
|
|
209 |
|
|
|
210 |
@param string $tag The name of the tag. For example 'div' for the `<div>` tag
|
|
|
211 |
@param mixed $content A string for tags such as `<title>string</title>`
|
|
|
212 |
@param bool $push A flag indicating if the tag should be pushed onto the stack
|
|
|
213 |
|
|
|
214 |
@return void does not return anything
|
|
|
215 |
*/
|
|
|
216 |
|
|
|
217 |
static public function tag($tag, $content = null, $push = true)
|
|
|
218 |
{
|
|
|
219 |
$has_content = !is_null($content); // the content can be an empty string
|
|
|
220 |
|
|
|
221 |
if ($push)
|
|
|
222 |
{
|
|
|
223 |
array_push(self::$stack, $tag);
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
self::indent();
|
|
|
227 |
self::write('<' . $tag);
|
|
|
228 |
self::expand_attributes();
|
|
|
229 |
self::write('>');
|
|
|
230 |
|
|
|
231 |
if ($push) self::$indent_level++;
|
|
|
232 |
|
|
|
233 |
if ($has_content) self::write($content);
|
|
|
234 |
|
|
|
235 |
if (!$has_content) self::write("\n");
|
|
|
236 |
|
|
|
237 |
if ($push && $has_content)
|
|
|
238 |
{
|
|
|
239 |
self::close(false);
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
Outputs a single tag such as `<img>` or `<input>`. Single tags do not require a closing tag.
|
|
|
247 |
|
|
|
248 |
@param string $tag The name of the tag. For example 'input' for an `<input>` tag
|
|
|
249 |
|
|
|
250 |
@return void does not return anything
|
|
|
251 |
|
|
|
252 |
*/
|
|
|
253 |
|
|
|
254 |
static public function single_tag($tag)
|
|
|
255 |
{
|
|
|
256 |
self::tag($tag, null, false);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
Closes the last opened tag. if $indent is set to false, there is no indentation, which is useful for tags where the open and close are on the same line like `<title>Title</title>`
|
|
|
263 |
|
|
|
264 |
@param bool $indent A flag controlling the indent. True for using indent
|
|
|
265 |
|
|
|
266 |
@return void does not return anything
|
|
|
267 |
*/
|
|
|
268 |
|
|
|
269 |
static public function close($indent = true)
|
|
|
270 |
{
|
|
|
271 |
|
|
|
272 |
$tag = array_pop(self::$stack);
|
|
|
273 |
|
|
|
274 |
self::$indent_level--;
|
|
|
275 |
if ($indent) self::indent();
|
|
|
276 |
self::write('</' . $tag . ">\n");
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
Returns the padding needed to indent a tag or string of text
|
|
|
283 |
|
|
|
284 |
@return void does not return anything
|
|
|
285 |
*/
|
|
|
286 |
|
|
|
287 |
static private function make_indent()
|
|
|
288 |
{
|
|
|
289 |
|
|
|
290 |
return str_repeat(' ', self::$indent_level * self::$spaces_per_indent);
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Performs the indentation by writing the padding returned by make_indent() to the output.
|
|
|
297 |
|
|
|
298 |
@return void does not return anything
|
|
|
299 |
*/
|
|
|
300 |
|
|
|
301 |
static private function indent()
|
|
|
302 |
{
|
|
|
303 |
if (self::$indent_level > 0) self::write(self::make_indent());
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
Appends some arbitrary code to the buffer or ouputs it after applying the current indent level before the code and each time a newline is found.
|
|
|
310 |
|
|
|
311 |
If properly indented code is passed, it will be appended and properly indented into the existing html code.
|
|
|
312 |
|
|
|
313 |
A newline is inserted after the code.
|
|
|
314 |
|
|
|
315 |
@param string $code The code to be appended
|
|
|
316 |
|
|
|
317 |
@return void does not return anything
|
|
|
318 |
*/
|
|
|
319 |
|
|
|
320 |
static public function insert_code($code)
|
|
|
321 |
{
|
|
|
322 |
|
|
|
323 |
$indent_factor = "\n" . self::make_indent();
|
|
|
324 |
|
|
|
325 |
self::indent();
|
|
|
326 |
$code = str_replace("\n", $indent_factor, $code);
|
|
|
327 |
|
|
|
328 |
self::write($code . "\n");
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
Shortcut function to write a `<br>` to the buffer
|
|
|
335 |
|
|
|
336 |
@return void does not return anything
|
|
|
337 |
*/
|
|
|
338 |
|
|
|
339 |
static public function br()
|
|
|
340 |
{
|
|
|
341 |
self::write('<br>');
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
Shortcut function to write a newline `\n` to the buffer
|
|
|
348 |
|
|
|
349 |
@return void does not return anything
|
|
|
350 |
*/
|
|
|
351 |
|
|
|
352 |
static public function nl()
|
|
|
353 |
{
|
|
|
354 |
self::write("\n");
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
/**
|
|
|
360 |
Shortcut function to write both `<br>` and `\n` to the buffer
|
|
|
361 |
|
|
|
362 |
@return void does not return anything
|
|
|
363 |
*/
|
|
|
364 |
|
|
|
365 |
static public function brnl()
|
|
|
366 |
{
|
|
|
367 |
self::br();
|
|
|
368 |
self::nl();
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
} // Ends html Class
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
?>
|