Creating links and forms

To create a link (<a>), use the Eliom_content.​Html5.​D.​a function, as in these examples:

open Eliom_content

 let links = Eliom_registration.Html5.register_service ["rep";"links"] Eliom_parameter.unit
 (fun () () ->
       (head (title (pcdata "Links")) [])
          [a coucou [pcdata "coucou"] (); br ();
           a hello [pcdata "hello"] (); br ();
           a default
             [pcdata "default page of the dir"] (); br ();
           a uasuffix
             [pcdata "uasuffix"] (2007,6); br ();
           a coucou_params
             [pcdata "coucou_params"] (42,(22,"ciao")); br ();
           a raw_serv
             [pcdata "raw_serv"] [("sun","yellow");("sea","blue and pink")]; br ();
                ~get_params:(suffix (all_suffix "suff"))
             [pcdata "OCaml on wikipedia"]
             ["OCaml"]; br ();
             ~a:[a_href (Raw.uri_of_string "")]
             [pcdata "OCaml on wikipedia"]


Eliom_content.​Html5.​D.​a takes as first parameter the service you want to link to. The third parameter is the text of the link. The last parameter is for GET parameters you want to put in the link. The type of this parameter and the name of GET parameters depend on the service you link to.

The links to Wikipedia shows how to define an external service (here it uses a suffix URL). For an external service without parameters, you can use the low level function Eliom_content.​Html5.​D.​Raw.​a, if you don't want to create an external service explicitly. Note that the path must be a list of strings: do not write ["foo/bar"], but ["foo";"bar"], otherwise, the "/" will be encoded in the URL.

If you want to create (mutually or not) recursive pages, create the service using Eliom_service.​App.​service or Eliom_service.​Http.​service first, then register it in the table using (for example) Eliom_registration.​Html5.​register:

let linkrec = Eliom_service.Http.service ["linkrec"] Eliom_parameter.unit ()

let _ = Eliom_registration.Html5.register linkrec
    (fun () () ->
        (head (title (pcdata "")) [])
        (body [p [a linkrec [pcdata "click"] ()]])))


(But you can also refer to the current service as Eliom_service.​void_coservice'.)


Forms towards GET services

The function Eliom_content.​Html5.​D.​get_form allows creation of forms that use the GET method (parameters in the URL). It works like Eliom_content.​Html5.​D.​a but takes a function that creates the form from the parameters names as parameter.

let create_form =
  (fun (number_name, (number2_name, string_name)) ->
      [p [pcdata "Write an int: ";
        int_input ~input_type:`Text ~name:number_name ();
        pcdata "Write another int: ";
        int_input ~input_type:`Text ~name:number2_name ();
        pcdata "Write a string: ";
        string_input ~input_type:`Text ~name:string_name ();
        string_input ~input_type:`Submit ~value:"Click" ()]]

let form = Eliom_registration.Html5.register_service ["form"] unit
  (fun () () ->
     let f = Html5.D.get_form coucou_params create_form in
         (head (title (pcdata "")) [])
         (body [f])))

See to see the function form>> in action]].

If you want to use typed parameters, you cannot use functions like Html5.​M.​input to create your forms (but you can use it if you want to use parameters defined with Eliom_parameter.​any). Indeed, parameter names are typed to force them be used properly. In our example, number_name has type int param_name and must be used with int_input (or other widgets), whereas string_name has type string param_name and must be used with string_input (or other widgets). All functions for creating form widgets are detailed in Eliom_content.​Html5.​D (and Eliom_content.​Html5.​D).

For untyped forms, you may use functions from Html5.​M or functions from the module Eliom_content.​Html5.​D.​Raw. Here is a form linking to our (untyped) service raw_serv.

let raw_form =
    (fun () () ->
                   (head (title (pcdata "")) [])
                      [h1 [pcdata "Any Form"];
                       get_form raw_serv
                         (fun () ->
                           [p [pcdata "Form to raw_serv: ";
                               raw_input ~input_type:`Text ~name:"plop" ();
                               raw_input ~input_type:`Text ~name:"plip" ();
                               raw_input ~input_type:`Text ~name:"plap" ();
                               raw_input ~input_type:`Submit ~value:"Click" ()]])

Try this form. >>

POST parameters

By default web page parameters are transferred in the URL (GET parameters). A web page may also expect POST parameters (that is, parameters that are not in the URL but in the body of the HTTP request, if the POST method is used).

let no_post_param_service =
    (fun () () ->
         (head (title (pcdata "")) [])
         (body [h1 [pcdata
                      "Version of the page without POST parameters"