6 |
](https://spatie.be/github-ad-click/laravel-html)
22 |
23 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
24 |
25 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
26 |
27 | ## Postcardware
28 |
29 | You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
30 |
31 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
32 |
33 | All postcards are published [on our website](https://spatie.be/en/opensource/postcards).
34 |
35 | ## Installation
36 |
37 | You can install the package via composer:
38 |
39 | ``` bash
40 | composer require spatie/laravel-html
41 | ```
42 |
43 | And optionally register an alias for the facade.
44 |
45 | ```php
46 | // config/app.php
47 | 'aliases' => [
48 | ...
49 | 'Html' => Spatie\Html\Facades\Html::class,
50 | ];
51 | ```
52 |
53 | ## Usage
54 |
55 | ### Concepts
56 |
57 | Elements—classes under the `Spatie\Html\Elements` namespace—are generally created via a `Spatie\Html\Html` builder instance.
58 |
59 | ```php
60 | html()->span()->text('Hello world!');
61 | ```
62 |
63 | Element attributes and contents are modified via with fluent methods which return a new instance. This means element instances are immutable.
64 |
65 | ```php
66 | $icon = html()->span()->class('fa');
67 |
68 | $icon->class('fa-eye'); // ''
69 | $icon->class('fa-eye-slash'); // ''
70 | ```
71 |
72 | Element classes don't have any knowledge of the outside world. Any coupling to other concepts, like requests and sessions, should happen in the builder class, not on the element classes.
73 |
74 | By convention, we assume that builder methods will modify values to our advantage (like pulling old values from the session on a failed form request), and element methods will be deterministic.
75 |
76 | ```php
77 | // This will try to resolve an initial value, and fall back to 'hello@example.com'
78 | $email = html()->email('email', 'hello@example.com');
79 |
80 | // This will always have 'hello@example.com' as it's value
81 | $email = html()->email('email')->value('hello@example.com');
82 | ```
83 |
84 | ## Upgrading
85 |
86 | ### From v1 to v2
87 |
88 | Version 2 was created because the typehints in version 1 was holding the package back in some cases (like multiple select which requires an array of values instead of a string which was assumed).
89 |
90 | Luckily, bumping the version number in `composer.json` and running `composer update` should be non-breaking. Here are some caveats to look out for:
91 |
92 | - The package now ships with a `html()` function by default, which returns an instance of the `Html` builder class. If you've defined your own method, you'll need to remove it.
93 | - Various type hints have been removed throughout the package, if you've extended a class to override its methods, you'll need to update them accordingly (everything still behaves the same!)
94 |
95 | ## Changelog
96 |
97 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
98 |
99 | ## Testing
100 |
101 | ```bash
102 | $ composer test
103 | ```
104 |
105 | ## Contributing
106 |
107 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
108 |
109 | ## Security
110 |
111 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.
112 |
113 | ## Credits
114 |
115 | - [Sebastian De Deyne](https://github.com/sebastiandedeyne)
116 | - [Freek Van der Herten](https://github.com/freekmurze)
117 | - [All Contributors](../../contributors)
118 |
119 | ## About Spatie
120 | Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).
121 |
122 | ## License
123 |
124 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
125 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to `laravel-html` will be documented in this file.
4 |
5 | ## 3.12.3 - 2025-12-22
6 |
7 | ### What's Changed
8 |
9 | * make BaseElement::unless() compatible with Conditionable::unless() by @miken32 in https://github.com/spatie/laravel-html/pull/260
10 |
11 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.12.2...3.12.3
12 |
13 | ## 3.12.2 - 2025-12-22
14 |
15 | ### What's Changed
16 |
17 | * Document conditional methods by @miken32 in https://github.com/spatie/laravel-html/pull/261
18 |
19 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.12.1...3.12.2
20 |
21 | ## 3.12.1 - 2025-10-02
22 |
23 | ### What's Changed
24 |
25 | * Update issue template by @AlexVanderbist in https://github.com/spatie/laravel-html/pull/257
26 | * Add support for multiples requests in the same process by @gtg-bantonio in https://github.com/spatie/laravel-html/pull/258
27 |
28 | ### New Contributors
29 |
30 | * @gtg-bantonio made their first contribution in https://github.com/spatie/laravel-html/pull/258
31 |
32 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.12.0...3.12.1
33 |
34 | ## 3.12.0 - 2025-03-21
35 |
36 | ### What's Changed
37 |
38 | * add disabled option to file input by @it-can in https://github.com/spatie/laravel-html/pull/253
39 |
40 | ### New Contributors
41 |
42 | * @it-can made their first contribution in https://github.com/spatie/laravel-html/pull/253
43 |
44 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.11.3...3.12.0
45 |
46 | ## 3.11.3 - 2025-02-17
47 |
48 | ### What's Changed
49 |
50 | * Laravel 12.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-html/pull/251
51 |
52 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.11.2...3.11.3
53 |
54 | ## 3.11.2 - 2025-02-05
55 |
56 | ### What's Changed
57 |
58 | * Radio buttons with value 0 are incorrectly marked as checked by @acarpio89 in https://github.com/spatie/laravel-html/pull/249
59 |
60 | ### New Contributors
61 |
62 | * @acarpio89 made their first contribution in https://github.com/spatie/laravel-html/pull/249
63 |
64 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.11.1...3.11.2
65 |
66 | ## 3.11.1 - 2024-10-18
67 |
68 | ### What's Changed
69 |
70 | * fix: Passing null to parameter #1 ($string) of type string is deprecated by @francoism90 in https://github.com/spatie/laravel-html/pull/244
71 |
72 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.11.0...3.11.1
73 |
74 | ## 3.11.0 - 2024-07-16
75 |
76 | ### What's Changed
77 |
78 | * Add some attributes by @francoism90 in https://github.com/spatie/laravel-html/pull/239
79 |
80 | ### New Contributors
81 |
82 | * @francoism90 made their first contribution in https://github.com/spatie/laravel-html/pull/239
83 |
84 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.10.1...3.11.0
85 |
86 | ## 3.10.1 - 2024-07-15
87 |
88 | ### What's Changed
89 |
90 | * Fix value omitted when input created with no name by @raveren in https://github.com/spatie/laravel-html/pull/235
91 | * Fix value omitted when input created with no name by @raveren in https://github.com/spatie/laravel-html/pull/236
92 | * Fix for Select and model's relationships by @nikosv in https://github.com/spatie/laravel-html/pull/237
93 |
94 | ### New Contributors
95 |
96 | * @nikosv made their first contribution in https://github.com/spatie/laravel-html/pull/237
97 |
98 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.10.0...3.10.1
99 |
100 | ## 3.10.0 - 2024-07-03
101 |
102 | ### What's Changed
103 |
104 | * Update docs for name attribute by @bskl in https://github.com/spatie/laravel-html/pull/225
105 | * Add Conditionable trait: now `->when()` helper is available on all elements by @raveren in https://github.com/spatie/laravel-html/pull/234
106 |
107 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.9.0...3.10.0
108 |
109 | ## 3.9.0 - 2024-04-25
110 |
111 | ### What's Changed
112 |
113 | * Add use statement by @bskl in https://github.com/spatie/laravel-html/pull/222
114 | * Add aria helper method by @bskl in https://github.com/spatie/laravel-html/pull/226
115 |
116 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.8.0...3.9.0
117 |
118 | ## 3.8.0 - 2024-04-24
119 |
120 | ### What's Changed
121 |
122 | * Add autocomplete attribute helper to the form element by @raveren in https://github.com/spatie/laravel-html/pull/221
123 | * Added support for Htmlable contents in BaseElement by @hemmesdev in https://github.com/spatie/laravel-html/pull/215
124 | * Register Service Provider in Laravel 11 by @gqrdev in https://github.com/spatie/laravel-html/pull/224
125 | * Add name attribute to form element by @bskl in https://github.com/spatie/laravel-html/pull/223
126 |
127 | ### New Contributors
128 |
129 | * @hemmesdev made their first contribution in https://github.com/spatie/laravel-html/pull/215
130 | * @gqrdev made their first contribution in https://github.com/spatie/laravel-html/pull/224
131 |
132 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.7.0...3.8.0
133 |
134 | ## 3.7.0 - 2024-03-23
135 |
136 | ### What's Changed
137 |
138 | * Fix return value in docs in element-methods.md by @raveren in https://github.com/spatie/laravel-html/pull/218
139 | * Add autocomplete attribute helper to input, select and textarea by @raveren in https://github.com/spatie/laravel-html/pull/219
140 | * Fix link with version in documentation by @fey in https://github.com/spatie/laravel-html/pull/217
141 |
142 | ### New Contributors
143 |
144 | * @raveren made their first contribution in https://github.com/spatie/laravel-html/pull/218
145 | * @fey made their first contribution in https://github.com/spatie/laravel-html/pull/217
146 |
147 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.6.0...3.7.0
148 |
149 | ## 3.6.0 - 2024-03-08
150 |
151 | ### What's Changed
152 |
153 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-html/pull/214
154 |
155 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.5.0...3.6.0
156 |
157 | ## 3.4.0 - 2024-01-05
158 |
159 | ### What's Changed
160 |
161 | * Fix docblock to solve phpstan errors when passing an array to html()->div() by @SanderMuller in https://github.com/spatie/laravel-html/pull/210
162 | * Documentation on how to extend the package by @azamtav in https://github.com/spatie/laravel-html/pull/204
163 |
164 | ### New Contributors
165 |
166 | * @SanderMuller made their first contribution in https://github.com/spatie/laravel-html/pull/210
167 | * @azamtav made their first contribution in https://github.com/spatie/laravel-html/pull/204
168 |
169 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.3.0...3.4.0
170 |
171 | ## 3.3.0 - 2023-10-24
172 |
173 | ### What's Changed
174 |
175 | - Add documentation for new FormElement::route() method by @miken32 in https://github.com/spatie/laravel-html/pull/190
176 | - Update `.gitattributes` by @totoprayogo1916 in https://github.com/spatie/laravel-html/pull/194
177 | - Correction to docs re: readonly vs isReadonly by @sgilberg in https://github.com/spatie/laravel-html/pull/195
178 | - Get value from model with casts php native enum by @bskl in https://github.com/spatie/laravel-html/pull/203
179 |
180 | ### New Contributors
181 |
182 | - @totoprayogo1916 made their first contribution in https://github.com/spatie/laravel-html/pull/194
183 | - @sgilberg made their first contribution in https://github.com/spatie/laravel-html/pull/195
184 | - @bskl made their first contribution in https://github.com/spatie/laravel-html/pull/203
185 |
186 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.2.2...3.3.0
187 |
188 | ## 3.2.2 - 2023-07-20
189 |
190 | ### What's Changed
191 |
192 | - Allow setting a form action to a route by @miken32 in https://github.com/spatie/laravel-html/pull/189
193 |
194 | ### New Contributors
195 |
196 | - @miken32 made their first contribution in https://github.com/spatie/laravel-html/pull/189
197 |
198 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.2.1...3.2.2
199 |
200 | ## 3.2.1 - 2023-01-24
201 |
202 | ### What's Changed
203 |
204 | - Convert all tests to Pest by @alexmanase in https://github.com/spatie/laravel-html/pull/183
205 | - Laravel 10.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-html/pull/184
206 |
207 | ### New Contributors
208 |
209 | - @alexmanase made their first contribution in https://github.com/spatie/laravel-html/pull/183
210 | - @laravel-shift made their first contribution in https://github.com/spatie/laravel-html/pull/184
211 |
212 | **Full Changelog**: https://github.com/spatie/laravel-html/compare/3.2.0...3.2.1
213 |
214 | ## 3.2.0 - 2022-12-27
215 |
216 | - Add `P` class to render paragraphs
217 |
218 | ## 3.1.0 - 2022-01-14
219 |
220 | - Allow Laravel 9
221 |
222 | ## 3.0.0 - 2021-11-17
223 |
224 | - Add compatiblity with PHP 8.1. The only breaking change with v2 is that `readonly` has been renamed to `isReadonly`.
225 |
226 | ## 2.30.0 - 2022-07-09
227 |
228 | - Better support for numeric values in attributes
229 |
230 | ## 2.29.0 - 2021-02-09
231 |
232 | - Add `target` attribute method to links and button
233 |
234 | ## 2.28.1 - 2020-11-30
235 |
236 | - add support for PHP 8
237 |
238 | ## 2.28.0 - 2020-09-30
239 |
240 | - add the disabled method to all elements that support the attribute (#165)
241 |
242 | ## 2.27.0 - 2020-09-09
243 |
244 | - Add support for Laravel 8
245 |
246 | ## 2.26.0 - 2020-04-20
247 |
248 | - Internal refactor to normalize availabel attribute methods
249 |
250 | ## 2.25.0 - 2020-03-02
251 |
252 | - add Laravel 7 support
253 |
254 | ## 2.24.0 - 2019-09-04
255 |
256 | - Added number input
257 |
258 | ## 2.23.0 - 2019-09-04
259 |
260 | - Laravel 6 support
261 | - Better handling for `0` values in inputs
262 | - Add `range` for range inputs
263 | - Format date and time values
264 |
265 | ## 2.22.1 - 2019-07-16
266 |
267 | - Prevent password fields to be filled
268 |
269 | ## 2.22.0 - 2019-04-26
270 |
271 | - Changed the `value` parameter in `data` to an optional parameter
272 |
273 | ## 2.21.0 - 2019-02-27
274 |
275 | - Added Laravel 5.8 support
276 | - Dropped PHP 7.0 support
277 | - Dropped Laravel 5.4 support
278 | - Dropped PHPUnit 6 support
279 |
280 | ## 2.20.1 - 2019-02-01
281 |
282 | - use `Arr::` and `Str::` functions
283 |
284 | ## 2.20.0 - 2019-01-18
285 |
286 | - Added `unless` method and magic `__call` handler (e.g. `$input->valueUnless(false, 5)`)
287 | - Added `size` attribute method to `Input`
288 | - Added `name` attribute method to `Button`
289 | - Fixed checkbox value repopulation after request
290 |
291 | ## 2.19.9 - 2019-01-10
292 |
293 | - Improve default of `tel` link
294 |
295 | ## 2.19.8 - 2018-09-04
296 |
297 | - Add support for Laravel 5.7
298 |
299 | ## 2.19.7 - 2018-04-30
300 |
301 | - Allow radio input check "0" value
302 |
303 | ## 2.19.6 - 2018-04-30
304 |
305 | - Correctly prefill form array attributes from the model
306 |
307 | ## 2.19.5 - 2018-04-04
308 |
309 | - Allow `null` children
310 |
311 | ## 2.19.4 - 2018-03-28
312 |
313 | - Revert comparison function change in `2.19.2`
314 |
315 | ## 2.19.2 - 2018-03-26
316 |
317 | - Fixed comparison function for selected options in `Select`
318 |
319 | ## 2.19.1 - 2018-03-23
320 |
321 | - Fixed `Html::radio` auto-generated id's & checked behaviour
322 |
323 | ## 2.19.0 - 2018-03-09
324 |
325 | - Changed `Input::require` to accept a boolean value
326 |
327 | ## 2.18.0 - 2018-03-02
328 |
329 | - Added `I` element class and `Html::i` factory method
330 |
331 | ## 2.17.0 - 2018-02-28
332 |
333 | - Added `Html::value` function that's a public method for `old`
334 |
335 | ## 2.16.0 - 2018-02-26
336 |
337 | - Added `Img` element class and `Html::img` factory method
338 |
339 | ## 2.15.1 - 2018-02-26
340 |
341 | - Removed `id` from CSRF fields
342 |
343 | ## 2.15.0 - 2018-02-23
344 |
345 | - Added `Input::date` and `Input::time`
346 |
347 | ## 2.14.0 - 2018-02-22
348 |
349 | - Added `Input::disabled`
350 |
351 | ## 2.13.1 - 2018-02-20
352 |
353 | - Added `Form::novalidate`
354 |
355 | ## 2.12.1 - 2018-02-08
356 |
357 | - Fixed Laravel 5.6 compatibility
358 |
359 | ## 2.12.0 - 2018-02-08
360 |
361 | - Added Laravel 5.6 compatibility
362 | - Fixed an issue with checkbox values
363 |
364 | ## 2.11.0 - 2018-02-02
365 |
366 | - Add `readonly` method to input
367 |
368 | ## 2.10.3 - 2018-01-09
369 |
370 | - Fix `__call` when using macros
371 |
372 | ## 2.10.2 - 2017-12-28
373 |
374 | - `Htmlable` elements can now be used in the `html()` method
375 | - Array notation is now implicitly converted to dot notation in `old` (e.g. `foo[1] -> foo.1`)
376 |
377 | ## 2.10.1 - 2017-12-18
378 |
379 | - Fixed old values containing `0`
380 |
381 | ## 2.10.0 - 2017-11-08
382 |
383 | - Added `required` method to `Select`
384 |
385 | ## 2.9.0 - 2017-10-20
386 |
387 | - Added `required` method to `Textarea`
388 |
389 | ## 2.8.2 - 2017-10-13
390 |
391 | - Fixed a bug with values that are a `"0"` string
392 |
393 | ## 2.8.1 - 2017-10-12
394 |
395 | - Fixed a bug with values that are a `"0"` string
396 |
397 | ## 2.8.0 - 2017-10-12
398 |
399 | - Added a magic `__call` method that responds to methods ending with `If`, so any method can be called with a condition as it's first argument. The method will only be applied if the condition is truthy.
400 |
401 | ## 2.7.0 - 2017-10-11
402 |
403 | - Added `BaseElement::data` for data attributes
404 |
405 | ## 2.6.0 - 2017-10-11
406 |
407 | - Added `BaseElement::setChildren` to replace all children
408 | - Fixed a bug that didn't select options in optgroups when applying a value
409 |
410 | ## 2.5.0 - 2017-10-11
411 |
412 | - Added `BaseElement::style` for setting the style attribute (with a string or an associative array)
413 | - Added `Html::reset` for form reset buttons
414 |
415 | ## 2.4.1 - 2017-09-07
416 |
417 | - Nothing changed, but `2.2.0` was accidentally tagged as `2.4.0`. This release contains the actual latest version at the time of writing.
418 |
419 | ## 2.3.0 - 2017-09-04
420 |
421 | - Added `checked` and `unchecked` methods to `Input`
422 |
423 | ## 2.2.0 - 2017-08-29
424 |
425 | - Added `Optgroup` element
426 | - Added the ability to create optgroups in `Options` by passing an array of groups with options
427 |
428 | ## 2.1.0 - 2017-08-24
429 |
430 | - Added `Html::file` and a `File` element for file inputs
431 |
432 | ## 2.0.2 - 2017-07-14
433 |
434 | - Fixed an issue that stripped square brackets from element attributes
435 |
436 | ## 2.0.1 - 2017-06-28
437 |
438 | - Fixed the `Html` facade
439 |
440 | ## 2.0.0 - 2017-06-13
441 |
442 | - Minimum requirements have been reduced to PHP 7.0
443 | - Added a `html()` helper function that returns an instance of `Html`
444 | - Added `Macroable` to all elements and `Html`
445 | - Loosened type hints in method signatures for flexibility
446 | - Added `Html::multiselect` method
447 | - Added `Select::multiple` method
448 |
449 | ## 1.5.0 - 2017-05-19
450 |
451 | - Added `class` method to `Html`
452 |
453 | ## 1.4.0 - 2017-05-16
454 |
455 | - Added a `placeholder` method to `Textarea`
456 |
457 | ## 1.3.1 - 2017-05-09
458 |
459 | - Added an empty `value` to `Select::placeholder`
460 |
461 | ## 1.3.0 - 2017-05-08
462 |
463 | - Added a `placeholder` method to `Select` for default empty values
464 |
465 | ## 1.2.0 - 2017-04-28
466 |
467 | - Added a `Html` facade
468 |
469 | ## 1.1.1 - 2017-04-27
470 |
471 | - Fixed an issue where html was escaped when you didn't want it to do that, like in buttons and links
472 |
473 | ## 1.1.0 - 2017-04-19
474 |
475 | - Added `Html::radio`
476 | - Fixed an issue that set the wrong `value` for a checkbox created with `Html::checkbox`
477 | - Fixed a case sensitivity issue with the `Textarea` class
478 |
479 | ## 1.0.0 - 2017-03-31
480 |
481 | - Initial release
482 |
--------------------------------------------------------------------------------
/src/BaseElement.php:
--------------------------------------------------------------------------------
1 | $attributes)
21 | * @method static attributesIfNotNull(bool $condition, iterable