1. Pierre Surply
  2. OCaml-WebFramework


OCaml-WebFramework / README.md

Pierre Surply fe52c9a 

# OCaml-WebFramework

## Template engine

The template engine, programmed with OCaml, is designed to process web templates (situated in `/website/templates/`) and dynamic content (situated in `/website/cgi-bin`) to produce output web documents.

The template language uses the following syntax :

  - `[begin *part*]*html code*[end]` : defines a part of html code
  - `{ *part* }` : includes the content of the part *part* previously defined
  - `{% *function* %}` : includes the result of the OCaml function *function*. The function must be declared in the file `/website/cgi-bin/cgi_functions.ml`.
  - `{@ *template* @}` : Tells the template engine that the current template "extends" another template.

Once template processing is complete, the content of the part `base` is printed by the CGI.

## URL dispatcher

The URL is divided as follows :


The arg0 represents the name of the page asked by the user.
You can link the arg0 to a template in the file `/website/cgi-bin/url.ml`.
The others arguments can be gotten with the function `Env.get_url_arg n`.

## Example

### templates/base.html

    [begin base]
    <!DOCTYPE html>
        <title>OCaml-OCR -{ title }</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <li><a href="?index/">Home</a></li>
            <li><a href="?about/">About</a></li>
        { body }

### templates/index.html

    {@ base.html @}
    [begin title] Index [end]
    [begin body]
    <h1>Hello World ! </h1>
    <p>{% get_time %}</p>

### templates/about.html

    {@ base.html @}
    [begin title] About [end]
    [begin body]

### cgi-bin/url.ml

    let get_page_from_url () =
      match Env.get_url_arg 0 with
        | "about" -> "about.html"
        | _ -> "index.html"

### cgi-bin/cgi_functions.ml

    let get_function = function
      | "get_time" -> Date.get_date ()
      | f -> "Cannot find function : " ^ f