package embedded_ocaml_templates

  1. Overview
  2. Docs
EML is a simple templating language that lets you generate text with plain OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

0.7.tar.gz
md5=c262b84b429163b6441e76554db18104
sha512=018abfb99112e355153ff00ecb5199921f01cd1311bc0943c54cba172fe66be72153f69433c3a3b440fecca02ca8be573b59b6419f4329e70dff6b2b67c66f7a

Description

Inspired by EJS templates, it does currently implements all of its functionnality. I plan to implement everything eventually, especially if someone actually want to use this. Please contact me if you find this interesting but there is a missing feature that you need !

Published: 30 Jun 2021

README

Embedded Ocaml Templates

EML is a simple templating language that lets you generate text with plain OCaml. The syntax is as follow :

First of all, you need to declare the template's arguments at the top of the template :

<%# arg1 (arg2:type) (arg3_1, arg3_2) %>

Then you can use two tags :

<% ocaml code here %>

This tag expect any ocaml code. If what you put in here is an expression of type unit, you should include the ";" yourself. You are able to open parenthesis and close them in a subsequent tag.

<%= ocaml expression here %>

This tag expect an expression of type string and is going to be replaced by the value of the expression, with HTML escaping. If this tag is inside a loop or an if statement, or any control structure, it's going to behave the way you would expect it to : outputting its content every time the branch is executed, with the right context.

This tag has a variant :

<%i= ocaml expression here %>

Here you can use any "simple" printf format specifier, where simple is defined by the following regex :

  'd' | 'i' | 'u' | 'n' | 'l' | 'N' | 'L' | 'x' | 'o' | 'X' | 's' | 'c'
| 'S' | 'C' | 'f' | 'e' | 'E' | 'g' | 'G' | 'h' | 'H' | 'b' | 'B'
| ('l' | 'n' | 'L'), ('d' | 'i' | 'u' | 'x' | 'X' | 'o')
| 't'

You can notice that <%s- x %> is equivalent to <%- x %>

You can use more complicated printf format specifiers with format flags, width and precision using the following syntax :

<%[i%]= ocaml expression here %>

Every time = is used to mark an outputting tag, it can be replaced by - to disable HTML escaping.

A slurp marker is also provided : <_% slurps whitespaces before it, and %_> after. It can be combined with output tags this way : <_%=.

Identifiers prefixed with __eml_ are reserved. This includes string delimiters {__eml_| and |__eml_}. Using them will not necessarily raise an error, but there is no guarantee if you do.

Because OCaml does not have an eval function, the templates have to be compiled. What is provided by this package is an executable that will compile either a single .eml file into an OCaml module containing a function that render the template, or take a whole directory containing a function for each .eml file and a submodule for each subdirectory (recursively).

Here is an exemple of a dune rule:

(rule
 (target templates.ml)
 (deps (source_tree templates))
 (action (run eml_compiler templates)))

There is also a ppx rewriter provided :

let name = "John"
let john = [%eml "<%-name%>"]

You can use the argument tag this way :

let user = [%eml "<%# name age %>name:<%-name%>, age:<%i- age%>"]

But in my opinion it is more elegant to write :

let user name age = [%eml "name:<%-name%>, age:<%i- age%>"]

There is also this nice new syntax that available from OCaml 4.11 onward :

let user name age = {%eml|name:<%-name%>, age:<%i- age%>|}

The ppx may be a bit slow at compile time, because I actually call the OCaml parser on generated code to build it. This has the advantage to be most likely compatible with future versions of OCaml, but I think it may be better to do it with some more standard tools such as metaquot.

Unfortunately, I have not managed to make the ppx positions correct.

Dependencies (9)

  1. ppx_inline_test
  2. containers
  3. ppxlib >= "0.8.0"
  4. pprint
  5. menhir >= "20180523"
  6. uutf
  7. sedlex >= "2.0"
  8. dune >= "2.8.0"
  9. ocaml >= "4.08.0"

Dev Dependencies

None

Used by

None

Conflicts

None