Module Litiom_choice

module Litiom_choice: sig .. end
Facilities for making select boxes more convenient and easier to use.

Eliom's select family of functions provides a powerful and complete toolbox for building select boxes in HTML5 forms. However, with great power comes great complexity, and select is notoriously cumbersome. This module aims to simplify the creation of select boxes for all those situations where the full power of Eliom's select is unnecessary. Moreover, it complements Litiom_type, on which it depends.

The module's facilities are available via functor Litiom_choice.Make. The functor's parameter must satisfy the Litiom_choice.CHOOSABLE signature, which is a superset of Litiom_type.STRINGABLE (alongside the components already present in Litiom_type.STRINGABLE, Litiom_choice.CHOOSABLE also requires the definition of a function describe mapping each element into a human-friendly description, and a value all with the enumeration of all members of the type). Finally, note that the functor's resulting signature Litiom_choice.S is a superset of Litiom_type.S.

As an example, let us create a module Suit satisfying Litiom_choice.S. Note the invocation of Litiom_choice.Make with a module satisfying the Litiom_choice.CHOOSABLE interface:

	module Suit = Litiom_choice.Make
		type t = Clubs | Spades | Diamonds | Hearts

		let of_string = function
			| "c" -> Clubs
			| "s" -> Spades
			| "d" -> Diamonds
			| "h" -> Hearts
			| x   -> invalid_arg ("Suit.of_string: " ^ x)

		let to_string = function
			| Clubs    -> "c"
			| Spades   -> "s"
			| Diamonds -> "d"
			| Hearts   -> "h"

		let describe = function
			| Clubs    -> "clubs (♣)"
			| Spades   -> "spades (♠)"
			| Diamonds -> "diamonds (♦)"
			| Hearts   -> "hearts (♥)"

		let all = [Clubs; Spades; Diamonds; Hearts]

Since Litiom_choice.S is a superset of Litiom_type.S, Suit may now be used in all circumstances described in Litiom_type. In addition, Suit also offers function Litiom_choice.S.choose, which makes the creation of select boxes as simple as is illustrated by the code below. (Note that function choose has other options, available via optional parameters.)

	let coucou_handler suit () =
			(head (title (pcdata "Coucou")) [])
			(body [p [pcdata "You have chosen "; pcdata (Suit.describe suit)]]))

	let coucou_service =
			~get_params:(Suit.param "suit")

	let coucou_form suit =
		label ~a:[a_for suit] [pcdata "Choose suit:"];
		Suit.choose ~name:suit ();
		button ~button_type:`Submit [pcdata "Submit"];

Module types

Bases for functors

module type DESCRIBABLE = sig .. end
module type ENUMERABLE = sig .. end
module type CHOOSABLE = sig .. end

Results of functors

module type S = sig .. end


module Make: 
functor (Base : CHOOSABLE) -> S with type t = Base.t