It is sometimes convenient to put some text in external file. HereDoc extends the syntax to provides four kind of includes: verbatim, templates, declaration includes and expression includes.
VERBATIM "file.txt"
returns a string with the content of the given file.
TPL "file.txt"
returns the content of the file, interpreted as a quotation (of
type Text.t).
INCLUDE "file.txt" (at the position of a toplevel declaration or
of a structure implementation item) includes the content of the
file.
EXPR "file.txt" same as INCLUDE, but for an expression.
To print a template, use:
Text.iter print_string TPL "file.txt"
You can put several chunks of text in a file. Use the notation:
"file.txt"."chunk"
to designate the section "chunk" in the file "file.txt", that is all the lines between the line "==chunk==" (two signs equal, the name of the chunk, two signs equal, alone on their line), and the line "====" (four signs equal).
Templates and expression includes may take arguments: if the opening ligne is ==chunk arg1 arg2 .. argn== instead of ==chunk==, the chunk returns a function with labeled arguments arg1,...,argn. For instance, if the template file is:
==add x y== x + y ==== ==link dest txt== <a href="$dest">$txt</a> ====
you can declare:
let add = EXPR "file.txt"."add" let link = TPL "file.txt"."link"
and get:
val add : x:int -> y:int -> int val link : dest:'a -> txt:'b -> Text.t
If many of the chunks you need are in the same file, you can use the toplevel phrase:
TEMPLATE_FILE "file.txt"
and then omit the file name, for instance:
TPL ."chunck"
When parsing an external template, the default file is the current file. By default, the template filename is "templates.tpl".
The commands "VERBATIM", "EXPR" and "TPL" are expressions; you can use them inside "expression in quotation". So you can for instance include a template from a template :
==chunk== Want to see another template ? $$TPL ."another"$$ ====
Author's mail : Alain.Frisch@ens.fr Author homepage: http://www.eleves.ens.fr:8080/home/frisch
Go to the first, previous, next, last section, table of contents.