Module Webs_html.Ht

HTML parts and elements.

HTML parts

type part

The type for HTML parts. A part is either a single element or character data or a list of parts.


type name = string

The type for element names.

val el : ?at:At.t list -> name -> part list -> part

el ?at n cs is an element with name n, attributes at (defaults to []) and children cs.

Except for At.class' the list at must not define an attribute more than once; this is not checked by the module. The At.class' is treated specially: multiple specifications are gathered to form a single, space separated, attribute value for the class HTML attribute.

See also (and favor) element constructors.


val txt : string -> part

txt d is character data d.

val sp : part

sp is Ht.txt " ".

val nbsp : part

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


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

splice ?sep ps when added to a list of children in v splices parts ps into the list, separating each part by sep (if any).

val void : part

void is splice [].

Raw data

val raw : string -> part

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

  • Include foreign markup.
  • With the style element, to avoid unpleasant surprises.


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

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

page ~lang ~generator ~styles ~scripts ~more_head ~title body is an Ht.html element with an At.lang attribute of lang (if specified and non-empty) containing a Ht.head element (see below) followed by body which must be a Ht.body element.

The other arguments are used to define the children of the page's Ht.head which are in order:

  1. A charset Ht.meta of UTF-8 (unconditional).
  2. A generator Ht.meta of generator, if specified an non-empty.
  3. A viewport Ht.meta with width=device-width, initial-scale=1 (unconditional).
  4. A stylesheet of type text/css for each element of styles, in order (defaults to []).
  5. A Ht.script with At.defer and of At.type' text/javascript for each element of scripts, in order (defaults to []).
  6. more_head (defaults to Ht.void). Be careful with style tags.
  7. The page has a title title which is String.trimed. If the result is empty falls back to "Untitled". See also title_of_filepath.
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 "", "Untitled". Directory separators can be '/' or '\\' regardless of the platform.


val buffer_add : doc_type:bool -> Stdlib.Buffer.t -> part -> unit

buffer_add ~doc_type b part adds part part. If doc_type is true an HTML doctype declaration is prepended.

val to_string : doc_type:bool -> part -> string

to_string is like buffer_add but returns directly a string.

module Low : sig ... end

Low level representation (unstable).

Element constructors

See the MDN HTML element reference.

Convention. Whenever an element name conflicts with an OCaml keyword we prime it, see for example object'.

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

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

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

The type for void element constructors. This is simply el 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 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 holds CSS use raw content, otherwise the CSS selector > 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