[ANN] Dream-html - DSL to build HTML, integrated with Dream

Hi, I have been working on a ‘simple’ OCaml library to generate HTML, with close integration with Dream. I am already using it on a side project and think it’s nearing production level for general use. I wanted to give a preview here in case anyone is interested.

In terms of approach taken, it is closer to Webs_html than to TyXML. Things that are statically typechecked:

  • ‘Standard’ tags have attributes and children
  • Void elements have no children
  • Some tags can contain only a single text node
  • Attributes with boolean, int, or enumerated string values
  • ‘Static’ in a sense: <html> tag is always automatically prefixed with <!DOCTYPE html>

All attributes and text nodes support format strings i.e. interpolation. So no need to pull in Printf.sprintf. HTML comments are also supported. HTML escaping is done for almost all text nodes and attribute values, leaving aside the ones that aren’t supported by Dream’s escaping i.e. JavaScript and CSS content.

Oh, one more thing. I also added the core htmx attributes since I’m using htmx.

Check out the test file for a slightly larger idiomatic usage: dream-html/test/dream_html_test.ml at main · yawaramin/dream-html · GitHub

16 Likes

Very nice! I like this combinator-based approach, I have a similar thing in tiny_httpd. I think it’s a nice balance between simplicity and expressiveness.

3 Likes

Just an update that this is now in opam repository: dream-html 0.0.3 (latest) · OCaml Package

Repo: GitHub - yawaramin/dream-html: Generate HTML markup from your Dream backend server

Docs: Dream_html (dream-html.Dream_html)

5 Likes

Just released 0.1.0 with a couple of small helpers:

  • Hx.__ can be used to render the _ attribute, which is used for Hyperscript
  • attr "attr-name" can be used to create a new attribute which does not carry a payload (what is usually called a boolean attribute)
1 Like