Readme

Mustache

Linux: Windows:

Mustache is

... a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.

This package ports over the mustache.js implementation for use in Julia. All credit should go there. All bugs are my own.

Examples

Following the main documentation for Mustache.js we have a "typical Mustache template" defined by:

using Mustache tpl = mt""" Hello {{name}} You have just won {{value}} dollars! {{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}} """

The values with braces (mustaches on their side) are looked up in a view, such as a dictionary or module. For example,

d = Dict( "name" => "Chris", "value" => 10000, "taxed_value" => 10000 - (10000 * 0.4), "in_ca" => true) render(tpl, d)

Yielding

Hello Chris You have just won 10000 dollars! Well, 6000.0 dollars, after taxes.

The render function pieces things together. Like print , the first argument is for an optional IO instance. In the above example, where one is not provided, a string is returned.

The flow is a template is parsed into tokens by Mustache.parse . This can be called directly, indirectly through the non-standard string literal mt , or when loading a file with Mustache.load . The templates use tags comprised of matching mustaches ( {} ), either two or three, to indicate a value to be substituted for. These tags may be adjusted when parse is called. The render function takes tokens as its second argument. If this argument is a string, parse is called internally. The render function than reassambles the template, substituting values, as appropriate, from the "view" passed to it and writes the output to the specified io argument.

The view used to provide values to substitute into the template can be specified in a variety of ways. The above example used a dictionary. A Module may also be used, such as Main :

name, value, taxed_value, in_ca = "Christine", 10000, 10000 - (10000 * 0.4), false render(tpl, Main) |> print

Which yields:

Hello Christine You have just won 10000 dollars!

Further, keyword arguments can be used when the variables in the templates are symbols:

goes_together = mt"{{{:x}}} and {{{:y}}}." render(goes_together, x="Salt", y="pepper") render(goes_together, x="Bread", y="butter")

Similarly, a named tuple may be used as a view. As well, one can use Composite Kinds. This may make writing show methods easier:

using Distributions tpl = "Beta distribution with alpha={{α}}, beta={{β}}" render(tpl, Beta(1, 2))

gives

"Beta distribution with alpha=1.0, beta=2.0"

Variables

Tags representing variables have the form {{varname}} , {{:symbol}} , or their triple-braced versions {{{varname}}} or {{{:symbol}}} . The triple brace prevents HTML substitution for entities such as < . The following are escaped when only double braces are used: "&", "", "'", "\", and "/".

If the variable refers to a function, the value will be the result of calling the function with no arguments passed in.

Sections

In the main example, the template included:

{{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}}

Tags beginning with #varname and closed with /varname create sections. The part between them is used only if the variable is defined.

If the section name refers to a function, that function will be passed the unevaluated string within the section, as expected by the Mustache specification. (If the tag "|" is used, the section value will be rendered first, an enhancement to the specification.)

Inverted

Related, if the tag begins with ^varname and ends with /varname the text between these tags is included only if the variable is not defined or is falsy .

Iteration

If the section variable, {{#VARNAME}} , binds to an iterable collection, then the text in the section is repeated for each item in the collection with the view used for the context of the template given by the item.

This is useful for collections of named objects, such as DataFrames (where the collection is comprised of rows) or arrays of dictionaries. For Tables.jl objects the rows are iterated over.

For data frames, the variable names are specified as symbols or strings. Here is a template for making a web page: