This library helps you create HTML.
74 |
94 | ->c(' Comment 2 ')
95 | ->region([], 'region with
tags')
96 | ->_()
97 | ->c(' Comment 3 ')
98 | ->a()
99 | ->c(' Comment 4 ')
100 | ->span(['class' => 'link'], 'Link content')
101 | ->_()
102 | ->div(['class' => 'Unsecure "classes"'], 'Unsecure content')
103 | ->_()
104 | ->c(' Comment 5 ');
105 |
106 | echo $html;
107 | ```
108 |
109 | This will produce:
110 |
111 | ```html
112 |
113 |
114 | some content
115 |
116 | this is a simple div
117 |
118 |
119 |
120 |
121 | region with <unsafe> tags
122 |
123 |
124 |
125 |
126 |
127 | Link content
128 |
129 |
130 |
131 | Unsecure <a href="#">content</a>
132 |
133 |
134 | ```
135 |
136 | ## Technical notes
137 |
138 | ### Tag analysis
139 |
140 | ```
141 | The tag name An attribute The content
142 | | | |
143 | ++-+ +-----+-----+ +----+-----+
144 | | | | | | |
145 |
146 | Hello world!
147 |
148 | | |
149 | +-----------------------+-----------------------+
150 | |
151 | The attributes
152 | ```
153 |
154 | The library is built around 3 objects.
155 |
156 | * The Tag object that handles the attributes, the tag name and the content,
157 | * The Attributes object that handles the attributes,
158 | * The Attribute object that handles an attribute which is composed of name and its value(s).
159 |
160 | The Tag object uses the Attributes object which is, basically, the storage of Attribute objects.
161 | You may use each of these objects individually.
162 |
163 | All methods are documented through interfaces and your IDE should be able to autocomplete when needed.
164 |
165 | Most methods parameters are [variadics](http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list) and
166 | accept unlimited nested values or array of values.
167 | You can also chain most of the methods.
168 |
169 | The allowed type of values can be almost anything. If it's an object, it must implements the `__toString()` method.
170 |
171 | #### Examples
172 |
173 | Method chaining:
174 |
175 | ```php
176 | attr('class', ['FRONT', ['NODE', ['sidebra']], 'node', ' a', ' b ', [' c']])
183 | ->replace('sidebra', 'sidebar')
184 | ->alter(
185 | function ($values) {
186 | $values = array_map('strtolower', $values);
187 | $values = array_unique($values);
188 | $values = array_map('trim', $values);
189 | natcasesort($values);
190 |
191 | return $values;
192 | }
193 | );
194 | $tag->content('Hello world');
195 |
196 | echo $tag; // Hello world
197 | ```
198 |
199 | The following examples will all produce the same HTML.
200 |
201 | ```php
202 | attr('class', ['front', ['node', ['sidebar']]]);
208 | $tag->content('Hello world');
209 |
210 | echo $tag; // Hello world
211 | ```
212 |
213 | ```php
214 | attr('class', 'front', 'node', 'sidebar');
220 | $tag->content('Hello world');
221 |
222 | echo $tag; // Hello world
223 | ```
224 |
225 | ```php
226 | attr('class', ['front', 'node', 'sidebar']);
232 | $tag->content('Hello world');
233 |
234 | echo $tag; // Hello world
235 | ```
236 |
237 | ```php
238 | attr('class', 'front node sidebar');
244 | $tag->content('Hello world');
245 |
246 | echo $tag; // Hello world
247 | ```
248 |
249 | ### Tag object
250 |
251 | ```php
252 | attr('class', 'front');
258 | $tag->content('Hello world');
259 |
260 | echo $tag; // Hello world
261 | ```
262 |
263 | ### Attributes object
264 |
265 | ```php
266 | append('class', 'a', 'b', 'c');
272 | $attributes->append('id', 'htmltag');
273 |
274 | // Hence the trailing space before the class attribute.
275 | echo $attributes; // class="a b c" id="htmltag"
276 | ```
277 |
278 | ### Attribute object
279 |
280 | ```php
281 | attr('class', 'E', 'C', ['A', 'B'], 'D', 'A', ' F ');
326 | // Add a random attribute and the same values.
327 | $tag->attr('data-original', 'e', 'c', ['a', 'b'], 'd', 'a', ' f ');
328 |
329 | echo $tag; //
330 | ```
331 |
332 | The same mechanism goes for the `Tag` class.
333 |
334 | ## Security
335 |
336 | To avoid security issues, every printed objects are escaped.
337 |
338 | If objects are used as input and if they implement the `__toString()` method, they will be converted to string.
339 | It's up to the user to make sure that they print **unsafe** output so they are not escaped twice.
340 |
341 | ## Code quality, tests and benchmarks
342 |
343 | Every time changes are introduced into the library, [Travis CI](https://travis-ci.org/drupol/htmltag/builds) run the tests and the benchmarks.
344 |
345 | The library has tests written with [PHPSpec](http://www.phpspec.net/).
346 | Feel free to check them out in the `spec` directory. Run `composer phpspec` to trigger the tests.
347 |
348 | [PHPBench](https://github.com/phpbench/phpbench) is used to benchmark the library, to run the benchmarks: `composer bench`
349 |
350 | [PHPInfection](https://github.com/infection/infection) is used to ensure that your code is properly tested, run `composer infection` to test your code.
351 |
352 | ## Contributing
353 |
354 | Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)
355 |
356 | [packagist collection]: https://packagist.org/packages/drupol/htmltag
357 | [latest stable version]: https://img.shields.io/packagist/v/drupol/htmltag.svg?style=flat-square
358 | [github stars]: https://img.shields.io/github/stars/drupol/htmltag.svg?style=flat-square
359 | [total downloads]: https://img.shields.io/packagist/dt/drupol/htmltag.svg?style=flat-square
360 | [github workflow status]: https://img.shields.io/github/workflow/status/drupol/htmltag/Continuous%20Integration?style=flat-square
361 | [code quality]: https://img.shields.io/scrutinizer/quality/g/drupol/htmltag/master.svg?style=flat-square
362 | [scrutinizer code quality]: https://scrutinizer-ci.com/g/drupol/htmltag/?branch=master
363 | [type coverage]: https://shepherd.dev/github/drupol/htmltag/coverage.svg
364 | [sheperd type coverage]: https://shepherd.dev/github/drupol/htmltag
365 | [code coverage]: https://img.shields.io/scrutinizer/coverage/g/drupol/htmltag/master.svg?style=flat-square
366 | [license]: https://img.shields.io/packagist/l/drupol/htmltag.svg?style=flat-square
367 | [donate github]: https://img.shields.io/badge/Sponsor-Github-brightgreen.svg?style=flat-square
368 | [donate paypal]: https://img.shields.io/badge/Sponsor-Paypal-brightgreen.svg?style=flat-square
369 | [github sponsor]: https://github.com/sponsors/drupol
370 | [paypal sponsor]: https://www.paypal.me/drupol
371 | [collection actions]: https://github.com/drupol/htmltag/actions
--------------------------------------------------------------------------------