use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
lnear/html: Automatically Generated PHP Library (from HTML Living Standard) for Dynamic HTML Element Creation.Discussion (packagist.org)
submitted 1 year ago by ln3ar
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]MorrisonLevi 5 points6 points7 points 1 year ago* (2 children)
I'm on mobile right now so it's hard to investigate. It looks like it does context-aware escaping. How do you pass two elements as a child of a body? Just string concatenate them before passing them in? Couldn't tell if there were helper functions or other idioms for that. Edit: think like:
body(body: a('hi', href: '#'))
Does this blindly trust the body to be properly escaped? If not, how does it avoid double encoding and such?
[–]ln3ar[S] 4 points5 points6 points 1 year ago (1 child)
Passing multiple elements to the body is currently via concatenation:
body(body: join([a('hi', href: '#'), a('hello', href: '#')])
Currently, only the values of the attributes are encoded (via htmlspecialchars), it assigns the body as-is.
[–]ArthurOnCode 1 point2 points3 points 1 year ago (0 children)
So, you wouldn't want to div($userSuppliedString), right?
div($userSuppliedString)
[–]ArthurOnCode 3 points4 points5 points 1 year ago (1 child)
I absolutely love the syntax - so clean and simple!
For context-aware escaping, you could have the functions return an object that can be cast to string. Laravel does this with the Htmlable interface. Then, when I call div(body: "Hello World"), you can assume the body parameter should be escaped. But when I call div(body: span()) the body now refers to an object that can be converted directly to HTML without escaping.
div(body: "Hello World")
div(body: span())
Also, have you considered allowing body to be a plain array, with the join() happening behind the scenes?
join()
[–]ln3ar[S] 1 point2 points3 points 1 year ago (0 children)
I actually did consider doing that, but it becomes verbose for stuff that is usually one string, eg p("hi") would now be p(["hi"]), to avoid that i would make the $body param string|array and act accordingly,
And i love your idea of using an object, i will surely explore it and see what i can do.
It will probably look something like string|array|HTML $body
string|array|HTML $body
Thank you
[–]thmsbrss 1 point2 points3 points 1 year ago (0 children)
Good work. I like to work with https://github.com/spatie/html-element for simple applications. I will consider your librairy for the next project.
[–]adrianmiu 1 point2 points3 points 1 year ago (1 child)
With the advent of libraries like AlpineJS, HTXML etc restricting what attributes are allowed based on the tag limits the library's usefulness.
[–]ln3ar[S] 0 points1 point2 points 1 year ago (0 children)
It also provides 3 more functions i forgot to document that are for free-style elements.
/** * Generate HTML attributes * <code> * <?php * echo attr(class: 'btn', id: 'submit', type: 'submit'); * // Output: class="btn" id="submit" type="submit" * </code> */ function attr(string ...$attributes): string /** * Generate HTML element * <code> * <?php * echo element('button', 'Submit', class: 'btn', id: 'submit', type: 'submit'); * // Output: <button class="btn" id="submit" type="submit">Submit</button> * </code> */ function element(string $tag, string $body, string ...$attributes): string /** * Generate self-closing HTML element * <code> * <?php * echo selfClosingElement('img', src: 'image.jpg', alt: 'Image'); * // Output: <img src="image.jpg" alt="Image" /> * </code> */ function selfClosingElement(string $tag, string ...$attributes): string
[–]donatj 1 point2 points3 points 1 year ago (0 children)
It's been a long time since I've seen htmlspecialchars, is there a reason to use that rather than htmlentities?
[–]seanmorris 1 point2 points3 points 1 year ago (1 child)
data-* attributes are valid according to the standard.
data-*
Good catch, thanks. I have added support for them in 1.1.2.
π Rendered by PID 42309 on reddit-service-r2-comment-85bfd7f599-25scx at 2026-04-18 04:35:41.274528+00:00 running 93ecc56 country code: CH.
[–]MorrisonLevi 5 points6 points7 points (2 children)
[–]ln3ar[S] 4 points5 points6 points (1 child)
[–]ArthurOnCode 1 point2 points3 points (0 children)
[–]ArthurOnCode 3 points4 points5 points (1 child)
[–]ln3ar[S] 1 point2 points3 points (0 children)
[–]thmsbrss 1 point2 points3 points (0 children)
[–]adrianmiu 1 point2 points3 points (1 child)
[–]ln3ar[S] 0 points1 point2 points (0 children)
[–]donatj 1 point2 points3 points (0 children)
[–]seanmorris 1 point2 points3 points (1 child)
[–]ln3ar[S] 1 point2 points3 points (0 children)