Grav v1.14.0

template

import "std/text" (included in import "std") · namespace std.text

Mustache templates over JSON data, HTML-escaped by default: pages, e-mails and messages filled in from a JsonValue. A template is parsed once and rendered as often as you like, from any number of threads.

grav
import "std"

fn main() {
    let t = Template.compile("""
        <h1>Hello {{name}}!</h1>
        <ul>
          {{#items}}
          <li>{{title}}: {{price}}</li>
          {{/items}}
        </ul>
        {{^items}}<p>Nothing yet.</p>{{/items}}
        {{> footer}}
        """).partial("footer", "<footer>{{shop.name}}</footer>")
    let data = Json.parse("{\"name\": \"<Ada>\", \"items\": [{\"title\": \"Tea\", \"price\": 2.5}, {\"title\": \"Cake\", \"price\": 4}], \"shop\": {\"name\": \"Kahawa & Co\"}}")
    print(t.render(data))
    print(Template.compile("Dear {{name}},").raw().render(data))
}
Output
<h1>Hello &lt;Ada&gt;!</h1>
<ul>
  <li>Tea: 2.5</li>
  <li>Cake: 4</li>
</ul>

<footer>Kahawa &amp; Co</footer>
Dear <Ada>,
  • Tags: {{name}} (escaped), {{{name}}} or {{& name}} (as it is), dotted names {{shop.name}}, {{.}} for the current value, {{#section}}…{{/section}} (a list repeats it; false, null, a missing value or an empty list skips it; anything else is entered as the new context), {{^inverted}}…{{/inverted}}, {{! comments }}, {{> partial}}, and {{=<% %>=}} to change the delimiters.
  • Lookup goes from the innermost context outward, so {{shop.name}} works inside {{#items}}.
  • Standalone lines: a section, comment or partial tag alone on its line takes the whole line with it, as the Mustache spec says, so templates can be indented freely.
  • raw() turns escaping off for plain-text output (e-mail bodies, SMS).
  • Errors: an unclosed section, a mismatched {{/name}} or an unclosed tag is a TemplateError whose message gives the line.
  • Numbers print as JSON writes them; true and false as words; objects and arrays as JSON.

API

class TemplateError extends Exception

A template that could not be parsed: an unclosed section, a stray closing tag.

Constructors

  • new TemplateError(m: string, line: int)

Fields

  • line: int — The 1-based line of the problem.

Methods

  • fn kind() -> string — "TemplateError".

class TplNode

Constructors

  • new TplNode(kind: int, text: string)

Fields

  • kind: int — 0 text, 1 escaped value, 2 raw value, 3 section, 4 inverted section, 5 partial
  • text: string
  • path: Array<string> = null
  • children: Array<TplNode> = null

class Template

A compiled Mustache template.

Constructors

  • new Template(nodes: Array<TplNode>)

Static members

  • static fn compile(source: string) -> Template — Parses source; TemplateError when a section is not closed or closed twice.
  • static fn renderString(source: string, data: JsonValue) -> string — compile and render in one step (for a template used once).

Methods

  • fn partial(name: string, partial: Template) -> Template — Makes {{> name}} render partial (with the data in scope where it appears).
  • fn partial(name: string, source: string) -> Template — partial, compiled from source.
  • fn raw() -> Template — Turns HTML escaping of {{name}} off (for plain-text output such as e-mail bodies).
  • fn render(data: JsonValue) -> string — The template filled in with data.
  • fn renderTo(sb: StringBuilder, data: JsonValue) — render, appended to sb.

class TplParser

Constructors

  • new TplParser(src: string)

Methods

  • fn parse() -> Array<TplNode>

Edit this page on GitHub