Giter Site home page Giter Site logo

embedded_ocaml_templates's Introduction

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.

This tag has a variant :

<%d- ocaml expression here %>

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

let format_flag = [%sedlex.regexp? '#' | '0' | '-' | '+']

let simple_format =
  [%sedlex.regexp?
    ( Opt format_flag,
      ( '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')
      | 'a' | 't' ) )]

You can use more complicated printf format specifiers using the following syntax :

<%(d%)- ocaml expression here %>

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 I think it may be better to do it with some more standard tools such as metaquot.

embedded_ocaml_templates's People

Contributors

emiletrotignon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.