126 |
- |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
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.
|
|
|
4 |
@author Jean-Georges Estiot <jgestiot@gmail.com>
|
|
|
5 |
@version 2.0
|
|
|
6 |
*/
|
127 |
- |
7 |
class Html {
|
|
|
8 |
private $spaces_per_indent;
|
|
|
9 |
private $stack;
|
|
|
10 |
private $attributes;
|
|
|
11 |
private $indent_level;
|
|
|
12 |
private $buffer;
|
|
|
13 |
private $buffered;
|
|
|
14 |
private $newline;
|
126 |
- |
15 |
|
127 |
- |
16 |
/**
|
|
|
17 |
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().
|
|
|
18 |
@param int $spaces The number of spaces to use in the indentation
|
|
|
19 |
@return void does not return anything
|
|
|
20 |
*/
|
126 |
- |
21 |
|
127 |
- |
22 |
public function init($spaces = 2) {
|
126 |
- |
23 |
|
127 |
- |
24 |
$this->spaces_per_indent = $spaces;
|
|
|
25 |
$this->newline = ($spaces == 0 ? "" : "\n");
|
|
|
26 |
$this->buffered = true;
|
|
|
27 |
$this->reset();
|
|
|
28 |
}
|
126 |
- |
29 |
|
127 |
- |
30 |
/**
|
|
|
31 |
Resets some of the key elements of the class: the stack, the indent, the attributes and the buffer.
|
|
|
32 |
@return void does not return anything
|
|
|
33 |
*/
|
126 |
- |
34 |
|
127 |
- |
35 |
public function reset() {
|
126 |
- |
36 |
|
127 |
- |
37 |
$this->stack = array();
|
|
|
38 |
$this->indent_level = 0;
|
|
|
39 |
$this->clear_attributes();
|
|
|
40 |
$this->clear_buffer();
|
|
|
41 |
}
|
126 |
- |
42 |
|
127 |
- |
43 |
/**
|
|
|
44 |
* Returns the content of the buffer and clears it
|
|
|
45 |
@return string The content of the buffer.
|
|
|
46 |
*/
|
126 |
- |
47 |
|
127 |
- |
48 |
public function flush() {
|
126 |
- |
49 |
|
127 |
- |
50 |
$temp = $this->buffer;
|
|
|
51 |
$this->clear_buffer();
|
|
|
52 |
return $temp;
|
|
|
53 |
}
|
126 |
- |
54 |
|
127 |
- |
55 |
/**
|
|
|
56 |
*clears the buffer
|
|
|
57 |
@return void does not return anything
|
|
|
58 |
*/
|
|
|
59 |
private function clear_buffer() {
|
126 |
- |
60 |
|
127 |
- |
61 |
$this->buffer = '';
|
|
|
62 |
}
|
126 |
- |
63 |
|
127 |
- |
64 |
/**
|
|
|
65 |
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.
|
|
|
66 |
@param bool $how True for buffered and false for unbuffered.
|
|
|
67 |
@return void does not return anything
|
|
|
68 |
*/
|
126 |
- |
69 |
|
127 |
- |
70 |
public function set_buffered($how) {
|
126 |
- |
71 |
|
127 |
- |
72 |
$this->buffered = $how;
|
|
|
73 |
}
|
126 |
- |
74 |
|
127 |
- |
75 |
/**
|
|
|
76 |
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.
|
|
|
77 |
@param string $string The string to write to output
|
|
|
78 |
@param bool $indent True to indent the string or false otherwise
|
|
|
79 |
@return void does not return anything
|
|
|
80 |
*/
|
126 |
- |
81 |
|
127 |
- |
82 |
public function write($string, $indent = false) {
|
126 |
- |
83 |
|
127 |
- |
84 |
if ($indent) $string = $this->make_indent() . $string;
|
|
|
85 |
if ($this->buffered) {
|
|
|
86 |
$this->buffer .= $string;
|
|
|
87 |
} else {
|
|
|
88 |
echo $string;
|
|
|
89 |
}
|
|
|
90 |
}
|
126 |
- |
91 |
|
127 |
- |
92 |
/**
|
|
|
93 |
Adds a single attribute to the next tag.
|
|
|
94 |
Example for an `<input>` tag: add_attribute('type','checkbox');
|
|
|
95 |
@param string $name The name of the attribute
|
|
|
96 |
@param string $value The value of the attribute
|
|
|
97 |
@return void does not return anything
|
|
|
98 |
*/
|
126 |
- |
99 |
|
127 |
- |
100 |
public function add_attribute($name, $value) {
|
126 |
- |
101 |
|
127 |
- |
102 |
$this->attributes[$name] = $value;
|
|
|
103 |
}
|
126 |
- |
104 |
|
127 |
- |
105 |
/**
|
|
|
106 |
Adds more than one attribute to the next tag
|
|
|
107 |
The attributes for the tag are passed in the format array('attribute'=>'value').
|
|
|
108 |
So array('id'=>'tag_id','class'=>'myclass') would produce this div tag:
|
|
|
109 |
`<div id="tag_id" class="myclass">`
|
|
|
110 |
@param array $ar The array holding the attributes
|
|
|
111 |
@return void does not return anything
|
|
|
112 |
*/
|
126 |
- |
113 |
|
127 |
- |
114 |
public function add_attributes($ar) {
|
126 |
- |
115 |
|
127 |
- |
116 |
if (is_array($ar)) {
|
126 |
- |
117 |
|
127 |
- |
118 |
$this->attributes = array_merge($this->attributes, $ar);
|
|
|
119 |
}
|
|
|
120 |
}
|
126 |
- |
121 |
|
127 |
- |
122 |
/**
|
|
|
123 |
Outputs the attributes for the tag that were added with add_attribute() or add_attributes().
|
|
|
124 |
Example: if the previously-added attributes are array('id'=>'tag_id','class'=>'myclass'), expand_attributes() will
|
|
|
125 |
produce the string 'id="tag_id" class="myclass'.
|
|
|
126 |
Important: all attributes are cleared once the string is output.
|
|
|
127 |
@return void does not return anything
|
|
|
128 |
*/
|
126 |
- |
129 |
|
127 |
- |
130 |
private function expand_attributes() {
|
126 |
- |
131 |
|
127 |
- |
132 |
foreach ($this->attributes as $idx => $val) {
|
|
|
133 |
$this->write(' ' . strtolower($idx) . '=' . '"' . $val . '"');
|
|
|
134 |
}
|
126 |
- |
135 |
|
127 |
- |
136 |
$this->clear_attributes(); //always clear attributes when finished
|
|
|
137 |
}
|
126 |
- |
138 |
|
127 |
- |
139 |
/**
|
|
|
140 |
Clears all attributes. (The next tag will have no attributes)
|
|
|
141 |
@return void does not return anything
|
|
|
142 |
*/
|
126 |
- |
143 |
|
127 |
- |
144 |
public function clear_attributes() {
|
126 |
- |
145 |
|
127 |
- |
146 |
$this->attributes = array();
|
|
|
147 |
}
|
126 |
- |
148 |
|
127 |
- |
149 |
/**
|
|
|
150 |
Generates a tag. There are three flavours:
|
|
|
151 |
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()
|
|
|
152 |
2) Tags that never require a close such as `<img href="image.jpg" />`
|
|
|
153 |
3) Tags that have content and are immediately terminated on the same line `<h1>Hello</h1>`
|
|
|
154 |
For the first, use tag('div');
|
|
|
155 |
For the second, use tag('img',false,false) or the function single_tag()
|
|
|
156 |
For the third, simply pass the tag's content like so: tag('h1','Hello')
|
|
|
157 |
@param string $tag The name of the tag. For example 'div' for the `<div>` tag
|
|
|
158 |
@param mixed $content A string for tags such as `<h1>string</h1>`
|
|
|
159 |
@param bool $push A flag indicating if the tag should be pushed onto the stack
|
|
|
160 |
@return void does not return anything
|
|
|
161 |
*/
|
126 |
- |
162 |
|
127 |
- |
163 |
public function tag($tag, $content = null, $push = true) {
|
|
|
164 |
$has_content = !is_null($content) && $content !== false; // the content can be an empty string
|
|
|
165 |
$has_single_tag = $content === false;
|
126 |
- |
166 |
|
127 |
- |
167 |
if ($push) {
|
|
|
168 |
array_push($this->stack, $tag);
|
|
|
169 |
}
|
126 |
- |
170 |
|
127 |
- |
171 |
$this->indent();
|
|
|
172 |
$this->write('<' . strtolower($tag));
|
|
|
173 |
$this->expand_attributes();
|
|
|
174 |
if ($has_single_tag) $this->write(" /");
|
|
|
175 |
$this->write('>');
|
126 |
- |
176 |
|
127 |
- |
177 |
if ($push) $this->indent_level++;
|
126 |
- |
178 |
|
127 |
- |
179 |
if ($has_content) $this->write($content);
|
126 |
- |
180 |
|
127 |
- |
181 |
if (!$has_content) $this->write($this->newline);
|
126 |
- |
182 |
|
127 |
- |
183 |
if ($push && $has_content) {
|
|
|
184 |
$this->close(false);
|
|
|
185 |
}
|
|
|
186 |
}
|
126 |
- |
187 |
|
127 |
- |
188 |
/**
|
|
|
189 |
Outputs a single tag such as `<img />` or `<input />`. Single tags do not require a closing tag.
|
|
|
190 |
@param string $tag The name of the tag. For example 'input' for an `<input />` tag
|
|
|
191 |
@return void does not return anything
|
|
|
192 |
*/
|
126 |
- |
193 |
|
127 |
- |
194 |
public function single_tag($tag) {
|
126 |
- |
195 |
|
127 |
- |
196 |
$this->tag($tag, false, false);
|
|
|
197 |
}
|
126 |
- |
198 |
|
127 |
- |
199 |
/**
|
|
|
200 |
Closes the last opened tag. if $indent is set to false, there is no indentation, which is useful for tags
|
|
|
201 |
where the open and close are on the same line like `<h1>Title</h1>`
|
|
|
202 |
@param bool $indent A flag controlling the indent. True for using indent
|
|
|
203 |
@return void does not return anything
|
|
|
204 |
*/
|
126 |
- |
205 |
|
127 |
- |
206 |
public function close($indent = true) {
|
126 |
- |
207 |
|
127 |
- |
208 |
$tag = array_pop($this->stack);
|
126 |
- |
209 |
|
127 |
- |
210 |
$this->indent_level--;
|
|
|
211 |
if ($indent) $this->indent();
|
|
|
212 |
$this->write('</' . $tag . ">" . $this->newline);
|
|
|
213 |
}
|
126 |
- |
214 |
|
127 |
- |
215 |
/**
|
|
|
216 |
Returns the padding needed to indent a tag or string of text
|
|
|
217 |
@return void does not return anything
|
|
|
218 |
*/
|
126 |
- |
219 |
|
127 |
- |
220 |
private function make_indent() {
|
126 |
- |
221 |
|
127 |
- |
222 |
return str_repeat(' ', $this->indent_level * $this->spaces_per_indent);
|
|
|
223 |
}
|
126 |
- |
224 |
|
127 |
- |
225 |
/**
|
|
|
226 |
* Performs the indentation by writing the padding returned by make_indent() to the output.
|
|
|
227 |
@return void does not return anything
|
|
|
228 |
*/
|
126 |
- |
229 |
|
127 |
- |
230 |
private function indent() {
|
126 |
- |
231 |
|
127 |
- |
232 |
if ($this->indent_level > 0) $this->write($this->make_indent());
|
|
|
233 |
}
|
126 |
- |
234 |
|
127 |
- |
235 |
/**
|
|
|
236 |
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.
|
|
|
237 |
If properly indented code is passed, it will be appended and properly indented into the existing html code.
|
|
|
238 |
A newline is inserted after the code. To avoid double newlines, a trailing newlines are removed from $code before any processing happens.
|
|
|
239 |
@param string $code The code to be appended
|
|
|
240 |
@return void does not return anything
|
|
|
241 |
*/
|
126 |
- |
242 |
|
127 |
- |
243 |
public function insert_code($code) {
|
126 |
- |
244 |
|
127 |
- |
245 |
$code = preg_replace('/\n$/','', $code);
|
|
|
246 |
$indent_factor = $this->newline . $this->make_indent();
|
126 |
- |
247 |
|
127 |
- |
248 |
$this->indent();
|
|
|
249 |
$code = str_replace("\n", $indent_factor, $code);
|
126 |
- |
250 |
|
127 |
- |
251 |
$this->write($code . $this->newline);
|
|
|
252 |
}
|
126 |
- |
253 |
|
127 |
- |
254 |
/**
|
|
|
255 |
Shortcut function to write a `<br>` to the buffer
|
|
|
256 |
@param bool $indent A flag controlling the indent. True for using indent
|
|
|
257 |
@return void does not return anything
|
|
|
258 |
*/
|
126 |
- |
259 |
|
127 |
- |
260 |
public function br($indent = false) {
|
126 |
- |
261 |
|
127 |
- |
262 |
$this->write('<br>', $indent);
|
|
|
263 |
}
|
126 |
- |
264 |
|
127 |
- |
265 |
/**
|
|
|
266 |
Shortcut function to write a newline `\n` to the buffer
|
|
|
267 |
@return void does not return anything
|
|
|
268 |
*/
|
126 |
- |
269 |
|
127 |
- |
270 |
public function nl() {
|
126 |
- |
271 |
|
127 |
- |
272 |
$this->write($this->newline);
|
|
|
273 |
}
|
126 |
- |
274 |
|
127 |
- |
275 |
/**
|
|
|
276 |
Shortcut function to write both `<br>` and `\n` to the buffer
|
|
|
277 |
@param bool $indent A flag controlling the indent. True for using indent
|
|
|
278 |
@return void does not return anything
|
|
|
279 |
*/
|
126 |
- |
280 |
|
127 |
- |
281 |
public function brnl($indent = true) {
|
126 |
- |
282 |
|
127 |
- |
283 |
$this->br($indent);
|
|
|
284 |
$this->nl();
|
|
|
285 |
}
|
126 |
- |
286 |
|
127 |
- |
287 |
} // Ends html Class
|