Module Htmlit.El

HTML elements and fragments.

See the element constructors and minimal page generation.

HTML fragments

type html

The type for HTML fragments. A fragment is either:


type name = string

The type for element names.

val v : ?at:At.t list -> name -> html list -> html

v ?at n cs is an element with name n, attributes at and children cs. Favour element constructors whenever possible. Regarding at:

  • at defaults to [].
  • Except for At.class' and attributes, at must not specify an attribute more than once. This is not checked by the module and what happens on multiple definitions is undefined.
  • For At.class' attributes, multiple specifications are gathered to form a single, space separated, attribute value for the class HTML attribute.
  • For attributes, multiple specifications are gathered to form a single, semi-colon separated, attribute value for the style HTML attribute.


val txt : string -> html

txt d is character data d.

val txt_of : ('a -> string) -> 'a -> html

txt_of f v is txt (f v). Cuts a bit on delimiter orgies.

val sp : html

sp is El.txt " ".

val nbsp : html

nbsp is El.txt "\u{00A0}".


val splice : ?sep:html -> html list -> html

splice ?sep hs is the list of fragments hs separated by sep (if any). When added to an element's children, the list is spliced in the element's children.

Unsafe raw data

val unsafe_raw : string -> html

unsafe_raw s is the raw string s without escaping markup delimiters. s must be well-formed HTML otherwise invalid markup is generated. This can be used to:

  • Include foreign markup, for example markup generated by another mechanism.
  • Avoid unpleasant surprises with the style element.
  • Let user-generated content create XSS attacks in your application.


Like void attributes, void fragments render nothing.

val void : html

void is splice []. See also is_void.

val is_void : html -> bool

is_void h is true iff h is an empty splice, an empty txt or an empty unsafe_raw. These fragments render nothing. See also void.


val buffer_add : doctype:bool -> Stdlib.Buffer.t -> html -> unit

buffer_add ~doctype b h adds the HTML fragment h to b. If doc_type is true an HTML doctype declaration is prepended.

val to_string : doctype:bool -> html -> string

to_string is like buffer_add but returns directly a string.

module Low : sig ... end

Low level representation (unstable).


There's more than one way to generate a basic minimal HTML page. The following provides good defaults for quick minimal pages.

val page : ?lang:string -> ?generator:string -> ?styles:string list -> ?scripts:string list -> ?more_head:html -> title:string -> html -> html

page ~lang ~title body is an El.html element with an At.lang attribute of lang (if specified and non-empty) containing a El.head element followed by body which must be a El.body element.

The children of the El.head element are in order:

  1. An El.meta charset with value utf-8, unconditional.
  2. An El.meta generator with value generator, if specified and non-empty.
  3. An El.meta viewport with value width=device-width, initial-scale=1, unconditional.
  4. For each non-empty element href of styles (defaults to []), an with At.type' text/css and At.href value href. In order.
  5. For each non-empty element src of scripts (defaults to []), an El.script with At.defer, At.type' value text/javascript and At.src value src. In order.
  6. more_head fragment (defaults to El.void). Be careful if you add style tags with direct CSS source.
  7. An El.title with value title which is String.trimed. If the result is empty falls back to "Untitled". See also title_of_filepath to derive titles from file paths.
val title_of_filepath : string -> string

title_of_filepath f is a non-empty page title for filepath f. Either the basename of f without extension or if that results in "index" or "" the basename of the parent directory without extension or if that results in "" the value "Untitled". Directory separators can be '/' or '\\' regardless of the platform.

Element constructors

See the MDN HTML element reference.

type cons = ?at:At.t list -> html list -> html

The type for element constructors. This is simply El.v with a pre-applied element name.

type void_cons = ?at:At.t list -> unit -> html

The type for void element constructors. This is simply El.v with a pre-applied element name and without children.

val a : cons
val abbr : cons
val address : cons
val area : void_cons
val article : cons
val aside : cons
val audio : cons
val b : cons
val base : void_cons
val bdi : cons
val bdo : cons
val blockquote : cons
val body : cons
val br : void_cons
val button : cons
val canvas : cons
val caption : cons
val cite : cons
val code : cons
val col : void_cons
val colgroup : cons
val command : cons
val datalist : cons
val dd : cons
val del : cons
val details : cons
val dfn : cons
val div : cons
val dl : cons
val dt : cons
val em : cons
val embed : void_cons
val fieldset : cons
val figcaption : cons
val figure : cons
val form : cons
val h1 : cons
val h2 : cons
val h3 : cons
val h4 : cons
val h5 : cons
val h6 : cons
val head : cons
val header : cons
val hgroup : cons
val hr : void_cons
val html : cons
val i : cons
val iframe : cons
val img : void_cons
val input : void_cons
val ins : cons
val kbd : cons
val keygen : cons
val label : cons
val legend : cons
val li : cons
val main : cons
val map : cons
val mark : cons
val menu : cons
val meta : void_cons
val meter : cons
val nav : cons
val noscript : cons
val object' : cons
val ol : cons
val optgroup : cons
val option : cons
val output : cons
val p : cons
val param : void_cons
val pre : cons
val progress : cons
val q : cons
val rp : cons
val rt : cons
val ruby : cons
val s : cons
val samp : cons
val script : cons
val section : cons
val select : cons
val small : cons
val source : void_cons
val span : cons
val strong : cons
val style : cons


Warning. If your style element contains CSS source, use unsafe_raw to specify it. Otherwise the CSS child combinator '>' gets escaped.

val sub : cons
val summary : cons
val sup : cons
val table : cons
val tbody : cons
val td : cons
val textarea : cons
val tfoot : cons
val th : cons
val thead : cons
val time : cons
val title : cons
val tr : cons
val track : void_cons
val u : cons
val ul : cons
val var : cons
val video : cons
val wbr : void_cons