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.3.1.tar.gz
md5=65dfebc97da6ad5038c6144a864a118d
sha512=98e42ba1e1e12311c434f88d8931cb308572f7ef96620517dec9d26a5ad5f8e3483dcdae7225c1d3c5f92ce178d0e73544b135a6d6f3e087e11c628d038261f9

Description

EML is a simple templating language that lets you generate text with plain OCaml Inspired by EJS templates

Published: 21 Jul 2020

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) %>

The 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. If this tag is inside a loop or an if statement, it's going to behave you would expect it to.

There are some identifiers that you cannot use : "append" and "elements" are going to be variables in the generated code. "{|" and "|}" are used as string delimiters. Using them will not necessarily raise an error, however I cannot guarantee what will happen 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 (it's recursive).

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%>"]

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 if someone would like to help me do that more cleanly I would appreciate the help.

Dependencies (8)

  1. containers
  2. ppxlib < "0.23.0"
  3. menhir >= "20180523"
  4. uutf
  5. core >= "v0.12"
  6. sedlex >= "2.0"
  7. dune >= "2.5.0"
  8. ocaml >= "4.08.0"

Dev Dependencies

None

Used by

None

Conflicts

None