Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

EmileTrotignon/embedded_ocaml_templates

Repository files navigation

EML is a simple templating language that lets you generate text with plain OCaml.It is analogous to the way you would write PHP pages, but the langage beingOcaml instead.

The syntax is as follow :

First of all, you can declare the template's arguments at the top of thetemplate :

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

This is optionnal, but this is the way to get ocaml values from the outside. Youcould also link a library that exposes the values.

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 typeunit, you should include the ";" yourself. You are able to open parenthesis andclose them in a subsequent tag.

<%= ocaml expression here %>

This tag expect an expression of type string and is going to be replaced by thevalue of the expression, with HTML escaping.If this tag is inside a loop or an if statement, or any control structure, it'sgoing 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 definedby 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, widthand precision using the following syntax :

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

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

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

Identifiers prefixed with__eml_ are reserved. This includes string delimiters{__eml_| and|__eml_}. Using them will not necessarily raise an error, butthere 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 asingle .eml file into an OCaml module containing a function that render thetemplate, or take a whole directory containing a function for each .eml file anda 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 :

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

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

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp