Creating links and forms

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

open Eliom_content

 let links = Eliom_registration.Html.create
   ~path:(Eliom_service.Path ["rep";"links"])
   ~meth:(Eliom_service.Get Eliom_parameter.unit)
 (fun () () ->
   Lwt.return
    Html.D.(
      html
       (head (title (pcdata "Links")) [])
       (body
         [p
          [a theservice [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 service_with_params
             [pcdata "Service with params"] (42,(22,"ciao")); br ();
           a raw_serv
             [pcdata "raw_serv"] [("sun","yellow");("sea","blue and pink")]; br ();
           a
             (Eliom_service.Http.extern
                ~prefix:"http://fr.wikipedia.org"
                ~path:["wiki";""]
                ~meth:(Eliom_service.Get (suffix (all_suffix "suff")))
                ())
             [pcdata "OCaml on wikipedia"]
             ["OCaml"]; br ();
           Raw.a
             ~a:[a_href (Raw.uri_of_string "http://en.wikipedia.org/wiki/OCaml")]
             [pcdata "OCaml on wikipedia"]
         ]])))

Eliom_content.​Html.​D.​a takes as first parameter the service you want to link to. The second 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 link 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.​Html.​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.​create first, then register it in the table using (for example) Eliom_registration.​Html.​register:

let linkrec =
  Eliom_service.create
    ~path:(Eliom_service.Path ["linkrec"])
    ~meth:(Eliom_service.Get Eliom_parameter.unit) ()

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

(You can also refer to the current service via reload_action (Eliom_service_sigs.​S.​reload_action).

Forms

Forms towards GET services

The modules Eliom_content.Html.D.Form and Eliom_content.Html.F.Form provide functions for creating forms. Our examples use shorthands to these modules, e.g., simply Form, assuming an appropriate context. See Eliom_content_sigs.​LINKS_AND_FORMS.​Form for the Form API documentation.

The function Eliom_content.Html.D.Form.get_form allows creation of forms that use the GET method (parameters in the URL). It works like Eliom_content.​Html.​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)) ->
    Html.D.(
      [p [pcdata "Write an int: ";
        Form.input ~input_type:`Text ~name:number_name Form.int;
        pcdata "Write another int: ";
        Form.input ~input_type:`Text ~name:number2_name Form.int;
        pcdata "Write a string: ";
        Form.input ~input_type:`Text ~name:string_name Form.string;
        Form.input ~input_type:`Submit ~value:"Click" Form.string]]
    ))

let form =
  Eliom_registration.Html.create
    ~path:(Eliom_service.Path ["form"])
    ~meth:(Eliom_service.Get Eliom_parameter.unit)
    (fun () () ->
       let f = Html.D.Form.get_form service_with_params create_form in
       Lwt.return
         (html
           (head (title (pcdata "")) [])
           (body [f])))

Parameter names are typed to make sure that they are used properly. The form-creating functions in Eliom_content.Html.D.Form (respectively Eliom_content.Html.F.Form) accept an argument of type Eliom_content.Html.D.Form.param (respectively Eliom_content.Html.F.Form.param), which needs to match the type of the parameter used. For example, number_name has type int param_name, so Form.int must be used with Form.input (or with the other widgets), whereas string_name has type string param_name and must be used in conjunction with the argument Form.string. Creating form widgets are described in detail in Eliom_content.Html.D.Form (and Eliom_content.Html.F.Form).

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

let raw_form =
  Eliom_registration.Html.create
    ~path:(Eliom_service.Path ["anyform"]