This is probably the one function you'll want to use. Expects a mustache template string and a JSON object to use as a hash.
import Json.Encode as E
render "Hello, {{world}}!" (E.object [("world", E.string "Pluto")]) --> Ok "Hello, Pluto!"Parse — but don't render — a template. Useful if you want to use the same template many times with different hashes.
Once a template has been parsed with parse it can be rendered with this function.
import Json.Encode as E
ast : Ast
ast = parse "Hello, {{.}}!" |> Result.withDefault []
renderParsed ast (E.string "Pluto") --> "Hello, Pluto!"
renderParsed ast (E.string "Mercury") --> "Hello, Mercury!"A string can be used as the template in a partial tag
import Json.Encode as E
template : String
template = """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
"""
partial : String
partial = "<strong>{{.}}</strong>"
render template
( E.object
[ ("names", E.list E.string [ "Alice", "Bob" ])
, ("user", E.string partial)
]
)
--> [ ""
--> , "<h2>Names</h2>"
--> , " <strong>Alice</strong>"
--> , " <strong>Bob</strong>"
--> , ""
--> , ""
--> ]
--> |> String.join "\n"
--> |> OkIt is not possible to directly inspect the Ast type as it contains implementation details. There is however the Tag type.
You can get data out of the tags with the following functions.
Get the name of a tag.
"Hello {{ world }}\n{{> body.contents }}"
|> parse
|> Result.withDefault []
|> tags
|> List.map name
--> [ ["world"], ["body", "contents"] ]A Mustahce tag name represented as a list.
The name foo.bar.baz is represented by ["foo", "bar", "baz"].
The implicit iterator {{.}} is represented by the empty list [].
Get the contents inside a section.
Get the contents inside an inverted section.
Escape the special HTML characters &, ", < and >. This function is used when rendering values in normal variable tags.
The context is a list of hashes (both of these terms are specified in the mustache specification) with the head of the list being the innermost hash.
Given a context, lookup a value in it. The lookup rules follows the ones outlined in the mustache specification.
Interpolate a JSON value into a string. This function is used when inserting a JSON value into a mustache variable.
Render a mustache section. This is only intended to be used by preparsed templates generated with the elm-mustache CLI tool
Render a mustache inverted section. This is only intended to be used by preparsed templates generated with the elm-mustache CLI tool
The AST type.