ECaml — a simple template engine for OCaml

ECaml logo

Download

Usage

% ecaml --help
ecaml - a simple template tool for OCaml
Usage: ecaml [OPTIONS] template.eml
  -o FILE destination file to output an OCaml code; default is sourcename.ml
  -p STR  printer function to apply to strings; default is print_string
  -esc-p STR  function to apply to <%= %> parts but not to <%=raw %>; default is the same as -p
  -d    write a directive with original file name for more impressive error messages
  -header STR header to write before the output
  -footer STR footer to write after the output
  -help  Display this list of options
  --help  Display this list of options

Syntax

This tool is so stupid, so don't forget about ; at the end of your expressions!

I don't want to add any other features yet, because I want to keep this tool simple.

Example

komar@thinkpad /home/komar/devel/ecaml % cat test.eml

<html>
<head></head>
<body>
<p><%= if Sys.os_type = "Unix" then "Hello world!" else "gtfo" %></p>
<ul>
% List.iter (fun s ->
<li><%= s %></li>
% ) ["hello"; "from"; "list"];
</ul>
</body>
</html>

komar@thinkpad /home/komar/devel/ecaml % ./ecaml test.eml
komar@thinkpad /home/komar/devel/ecaml % cat test.ml

print_string "<html>\n";
print_string "<head></head>\n";
print_string "<body>\n";
print_string "<p>";print_string ( if Sys.os_type = "Unix" then "Hello world!" else "gtfo" );print_string "</p>\n";
print_string "<ul>";
 List.iter (fun s ->
print_string "<li>";print_string ( s );print_string "</li>";
 ) ["hello"; "from"; "list"];
print_string "</ul>\n";
print_string "</body>\n";
print_string "</html>\n";

komar@thinkpad /home/komar/devel/ecaml % ocaml test.ml

<html>
<head></head>
<body>
<p>Hello world!</p>
<ul><li>hello</li><li>from</li><li>list</li></ul>
</body>
</html>

And how to use this stuff with ocsigen?

Write something like:

% let f buf ~title ~content = (
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
    <title><%= title %></title>
  </head>
  <body>
    <p><%= content %></p>
  </body>
</html>
% )

Then compile this template with -p 'Buffer.add_string buf':

komar@thinkpad /home/komar % ~/devel/ecaml/ecaml -p 'Buffer.add_string buf' site_template.eml
komar@thinkpad /home/komar % cat site_template.ml

 let f buf ~title ~content = (
Buffer.add_string buf "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
Buffer.add_string buf "  <head>\n";
Buffer.add_string buf "    <meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\" />\n";
Buffer.add_string buf "    <title>";Buffer.add_string buf ( title );Buffer.add_string buf "</title>\n";
Buffer.add_string buf "  </head>\n";
Buffer.add_string buf "  <body>\n";
Buffer.add_string buf "    <p>";Buffer.add_string buf ( content );Buffer.add_string buf "</p>\n";
Buffer.add_string buf "  </body>\n";
Buffer.add_string buf "</html>";
 )

And now you can get the output with:

let title = "hi" in
let content = "nothing to do there" in
let buf = Buffer.create 666 in
Site_template.f buf ~title ~content;
Buffer.contents buf

See also