A Racket implementation of Mustache.
$ raco pkg install "git://github.com/adolfopa/racket-mustache.git?path=mustache"
Lets create a simple Mustache template that outputs a single value. First, create
a file named simple.ms
(the .ms
extension is not required; you can use whatever
you want).
#lang mustache
Hey! this is my {{adjective}} Mustache template.
Note that templates must have #lang mustache
as the first thing in the file for
Racket to detect it as a Mustache template.
Templates are compiled into regular racket modules. To invoke the template from another
module, simply require it and call the render
function. Type this into a simple-test.rkt
file in the same directory where simple.ms
is:
#lang racket
(require "simple.ms") ; imports the `render` function
(module+ main
(render (hash "adjective" "first") (current-output-port)))
The render function has the following signature:
(: render ((Dict String Any) Port -> Void))
where the first argument is a dict mapping names (strings) to values (any racket value); the second argument is the port where the template will write its output.
If you execute the simple-test.rkt
script you should see something like:
$ racket simple-test.rkt
Hey! this is my first Mustache template.
And that's it!