mustache.tcl is a Tcl-only implementation of the mustache templating language.
This package supports the v1.1.3 mustache spec which can be found at https://github.com/mustache/spec/tree/master/specs, lambdas are supported, too.
See https://github.com/mustache for further information on mustache.
There is only a single command
::mustache::mustache template data ?libraryvar ...?
which takes a template and a context data dict and returns the rendered text.
set text {
<h1>Dear {{firstname}},{{! This is a comment}}</h1>
<p>Best wishes to your {{age}} birthday.</p>
<h2>{{sender}}</h2>
}
set data [dict create firstname "Fred" age "40th" sender "Greg & Tina"]
puts [::mustache::mustache $text $data]
Partials are supplied from variables in the current scope. If no variable is found, the libraryvars are consulted. These are dict variables where the key is matched against the partial name.
Lamdba use is straightforward. Just make a context value a list with "Λtcl" as first element to declare the next element as an executeable content.
To work around the limitation Tcl doesn't support explicit typing and thus,
lambdas an inherently unsafe with user supplied content (and aside from
the usual Tcl way of sandboxing anything unsafe), there is support for
"unsafe" sections. These are ordinary sections with an "unsafe" marker on
start of their context. Lambdas within those sections are always substituted
from the context *outside* the section. This makes it safe to use programmer
defined lambdas mixed with user content.
The lambda prefix can be changed globally, should this ever be needed, by doing
set ::mustache::LambdaPrefix "Λtcl"
The lambda unsafe marker can be changed globally, should this ever be needed, by doing
set ::mustache::LambdaUnsafe "λtcl"
See examples.tcl.
The package passes all the mandatory tests from the testsuite without modifications. You can update the yaml files in the tests directory to check against a newer version of the mustache spec. Call
tests.tcl
to run the testsuite. A single argument denoting the test number to run is also supported
The ~lambdas.yml file from the spec had to be tweaked, as there hadn't been any Tcl lambdas in there. Second, Tcl doesn't support explicit typing, so tcllib's yaml suite doesn't support the !code modifier. Instead a lambda has to be marked with Λtcl (that's an uppercase lambda in front of the "tcl"), which acts as a magic string making mustache.tcl detecting a lambda.
The additional test file ~lambdas-unsafe.yml is supported to test the unsafe sections feature unique to mustache.tcl.
Partials indentation was buggy <= v1.1.2.2, now it's fixed, I think. If you found another bug or have more testcases, please contact me.
Jan Kandziora jjj@gmx.de
(C)2015 by Jan Kandziora jjj@gmx.de
You may use, copy, distibute, and modify this software under the terms of the BSD-2-Clause license. See file COPYING for details.