Files
swiftpm-mustache/documentation/Mustache Syntax.md
Adam Fowler b331d4c440 Documentation (#12)
* Create Mustache Syntax.md

* Update Mustache Syntax.md

* Create Transforms.md

* Update Transforms.md

* Update Mustache Syntax.md

* Update Mustache Syntax.md

* Create Lambdas.md

* Update Lambdas.md
2021-03-24 18:13:37 +00:00

3.1 KiB

Mustache Syntax

Mustache is a "logic-less" templating engine. The core language has no flow control statements. Instead it has tags that can be replaced with a value, nothing or a series of values. Below we document all the standard tags

Context

Mustache renders a template with a context stack. A context is a list of key/value pairs. These can be represented by either a Dictionary or the reflection information from Mirror. For example the following two objects will render in the same way

let object = ["name": "John Smith", "age": 68]
struct Person {
    let name: String
    let age: Int
}
let object = Person(name: "John Smith", age: 68)

Initially the stack will consist of the root context object you want to render. When we enter a section tag we push the associated value onto the context stack and when we leave the section we pop that value back off the stack.

Tags

All tags are surrounded by a double curly bracket {{}}. When a tag has a reference to a key the associated value will be searched for from the context at the top of the context stack. If the value cannot be found then the next context down will be searched and so on until either a value is found or we have reached the bottom of the stack. If no value is found the output for that value is nil.

Tag types

  • {{key}}: Render value associated with key as text. By default this is HTML escaped. A nil value is rendered as an empty string.
  • {{{name}}}: Acts the same as {{name}} except the resultant text is not HTML escaped. You can also use {{&name}} to avoid HTML escaping.
  • {{#section}}: Section render blocks either render text once or multiple times depending on the value of the key in the current context. A section begins with {{#section}} and end with {{/section}}. If the key represents a Bool value it will only render if it is true. If the key represents an Optional it will only render if the object is non-nil. If the key represents an Array it will then render the internals of the section multiple times, once for each element of the Array. Otherwise it will render with the selected value pushed onto the top of the context stack.
  • {{^section}}: An inverted section does the opposite of a section. If the key represents a Bool value it will render if it is false. If the key represents an Optional it will render if it is nil. If the key represents a Array it will render if the Array is empty.
  • {{! comment }}: This is a comment tag and is ignored.
  • {{> partial}}: A partial tag renders another mustache file, with the current context stack. In Hummingbird Mustache partial tags only work for templates that are a part of a library and the tag is the name of the referenced file without the ".mustache" extension.
  • {{=<% %>=}}: The set delimiter tag allows you to change from using the double curly brackets as tag delimiters. In the example the delimiters have been changed to <% %> but you can change them to whatever you like.

You can find out more about the standard Mustache tags in the Mustache Manual.