Moustachu is a(N im)plementation of Mustache (get it?). Mustache is "logic-less templating".
In code
import nim_moustachu
var tmplate = """Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}"""
var c : Context = newContext()
c["name"] = "Chris"
c["value"] = 10000
c["taxed_value"] = 10000 - (10000 * 0.4)
c["in_ca"] = true
echo render(tmplate, c)
For other mustache examples look at the specs
directory. For other moustachu-specific examples see the tests
folder.
For the formal description of the mustache format, please visit mustache(5). Ignore the sections on "lambdas" and "set delimeters".
Not mentioned in the formal description (but mentioned in the spec code), the spec also supports using a dot .
as an "implicit iterator" for arrays containing unnamed items. For example, a sequence of strings or integers would use an implicit iterator:
import nim_moustachu
var c : Context = newContext()
c["zoo_name"] = "Anytown"
c["animals"] = @["lions", "tigers", "bears"]
var tmplate = """Animals at the {{zoo_name}} Zoo:
{{#animals}}
* {{.}}
{{/animals}}"""
echo render(tmplate, c)
The first version will print to stdout and the second will generate a file.
Moustachu supports the specs found in its specs directory:
- comments
- interpolation
- inverted
- partials
- sections
The recommended way to install moustachu is through nimble:
Install nimble. Then do:
$ nimble install https://github.com/zendbit/nim_moustachu@#head
- Make the interfaces with the data structures as dynamic-like as possible
- No lambdas, nor set delimiters. At least for now. Let's keep it simple please.
- Test in context. Tests are run on the installed package because that is what people get.
Get the source code:
$ git clone https://github.com/zendbit/nim_moustachu.git
$ cd nim_moustachu
# make your changes ...
# test
$ nimble tests
# run benchmarks
$ nimble benchmarks
This will test against the selected specs copied from mustache/spec
- Use to see what else to do/fix