How does a page's source code look?

Application

With Ocsigen, your website is an application (or several) running in your browser for a certain lifetime. An application has its associated js file, which must have the same name (provided in the Makefile).

For example, we define an application called example:

module Example =
  Eliom_registration.App
    (struct
      let application_name = "example"
     end)

Ocsigen Services

Ocsigen pages are made using services. Every pages have their own services.

So, to create a page, we have to first declare the service:

let main =
  Eliom_service.service
    ~path:[]
    ~get_params:unit
    ()

The path is a list of string corresponding to the url. Here, the list is empty because it's the main page.

If, for example, the list is ["hello";"world"], the page will be accessed using the address:

http://website.com/hello/world/

More informations about parameters: How to use "get" parameters or parameters in the URL?

Now that our service has been declared, we must define it by registering it. We must register it in the application we create before.

let _ =
  Example.register
    ~service:main
    (fun () () ->
      Lwt.return ...)

The register function take a function as a parameter. This function will be called when the service will be called (in that case, when we want to display the page).

This function take two parameters (the given parameters - here, unit, as said in the declaration - and unit) and return an element of type html.

Let's have a closer look on this html element...

Page Content

The content of Ocsigen pages is made using functions. The html function have two parameters: head and body, which are also functions that takes parameters coresponding to their content.

(html
           (head (title (pcdata "Page Title")) [])
           (body [p [pcdata "Hello World!"]])))

Most of the elements functions take a list of other elements as a parameter.

This is how the AST (Abstract Syntax Tree) is made!

ast

Download full code

Links

◄ Back to the Quick Howto's