How to use "get" parameters or parameters in the URL?

All pages are services (How does a page's source code look?). These services has two parameters: path and get_params.

path

This argument is a list of string, corresponding to the URL where your page service can be found.

For example, if you want your page to be accessed via this URL:

http://website.com/a/b/c

You must provide this path:

~path:["a";"b";"c"]

This parameter can also be an empty list. It means that the service page will be accessed via this URL:

http://website.com/

get_params

Services can have several parameters of several types.

Don't forget to open the module Eliom_parameter at the beginning of your file!

Syntax for several parameters

~get_params:unit
~get_params:(int "id")
~get_params:(string "name" ** int "age" ** bool "graduate")

Types of parameters

Get these parameters

On service registration, parameters can be retrieved as a parameter of the function:

Example.register ~service:the_service
    (fun parameters () -> ...)
(fun () () -> ...)
(fun a () -> ...)

The operator is left associative:

(fun (a, (b, (c, d)))) () -> ...)

Variable paths

Parameters provided in the path are constant. It's possible to have a variable URL!

Imagine you want to offer a personal page to your users. You will need a variable URL that will look like this, where xxx can be replace by the username:

http://website.com/users/xxx

To do that, add a parameter of type suffix in get_params:

~get_params:(suffix (string "username"))

Download full code

Links

◄ Back to the Quick Howto's