Home

Awesome

GRMustache.swift Swift Platforms License

Mustache templates for Swift

Latest release: August 19, 2024 • version 5.0.0 • CHANGELOG

Requirements: iOS 8.0+ / OSX 10.10+ / tvOS 9.0+ • Xcode 16+ • Swift 5

Follow @groue on Twitter for release announcements and usage tips.


<p align="center"> <a href="#features">Features</a> &bull; <a href="#usage">Usage</a> &bull; <a href="#installation">Installation</a> &bull; <a href="#documentation">Documentation</a> </p>

Features

GRMustache extends the genuine Mustache language with built-in goodies and extensibility hooks that let you avoid the strict minimalism of Mustache when you need it.

Usage

The library is built around two main APIs:

document.mustache:

Hello {{name}}
Your beard trimmer will arrive on {{format(date)}}.
{{#late}}
Well, on {{format(realDate)}} because of a Martian attack.
{{/late}}
import Mustache

// Load the `document.mustache` resource of the main bundle
let template = try Template(named: "document")

// Let template format dates with `{{format(...)}}`
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
template.register(dateFormatter, forKey: "format")

// The rendered data
let data: [String: Any] = [
    "name": "Arthur",
    "date": Date(),
    "realDate": Date().addingTimeInterval(60*60*24*3),
    "late": true
]

// The rendering: "Hello Arthur..."
let rendering = try template.render(data)

Installation

CocoaPods

CocoaPods is a dependency manager for Xcode projects.

To use GRMustache.swift with CocoaPods, specify in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'GRMustache.swift'

Carthage

Carthage is another dependency manager for Xcode projects.

To use GRMustache.swift with Carthage, specify in your Cartfile:

github "groue/GRMustache.swift"

Swift Package Manager

The Swift Package Manager is the open source tool for managing the distribution of Swift code.

To use GRMustache.swift with the Swift Package Manager, add https://github.com/groue/GRMustache.swift to the list of your package dependencies:

dependencies: [
    .package(url: "https://github.com/groue/GRMustache.swift", from: "6.0.0")
]

Add Mustache to your target:

targets: [
    .target(
        name: "MyTool",
        dependencies: ["Mustache"])
]

Manually

  1. Download a copy of GRMustache.swift.

  2. Checkout the latest GRMustache.swift version:

    cd [GRMustache.swift directory]
    git checkout 6.0.0
    
  3. Embed the Mustache.xcodeproj project in your own project.

  4. Add the MustacheOSX, MustacheiOS, or MustacheWatchOS target in the Target Dependencies section of the Build Phases tab of your application target.

  5. Add the the Mustache.framework from the targetted platform to the Embedded Binaries section of the General tab of your target.

See MustacheDemoiOS for an example of such integration.

Documentation

To fiddle with the library, open the Xcode/Mustache.xcworkspace workspace: it contains a Mustache-enabled Playground at the top of the files list.

External links:

Rendering templates:

Feeding templates:

Misc:

Loading Templates

Templates may come from various sources:

For more information, check:

Errors

Not funny, but they happen. Standard errors of domain NSCocoaErrorDomain, etc. may be thrown whenever the library needs to access the file system or other system resource. Mustache-specific errors are of type MustacheError:

do {
    let template = try Template(named: "Document")
    let rendering = try template.render(data)
} catch let error as MustacheError {
    // Parse error at line 2 of template /path/to/template.mustache:
    // Unclosed Mustache tag.
    error.description
    
    // templateNotFound, parseError, or renderError
    error.kind
    
    // The eventual template at the source of the error. Can be a path, a URL,
    // a resource name, depending on the repository data source.
    error.templateID
    
    // The eventual faulty line.
    error.lineNumber
    
    // The eventual underlying error.
    error.underlyingError
}

Mustache Tags Reference

Mustache is based on tags: {{name}}, {{#registered}}...{{/registered}}, {{>include}}, etc.

Each one of them performs its own little task:

Variable Tags

A Variable tag {{value}} renders the value associated with the key value, HTML-escaped. To avoid HTML-escaping, use triple mustache tags {{{value}}}:

let template = try Template(string: "{{value}} - {{{value}}}")

// Mario &amp; Luigi - Mario & Luigi
let data = ["value": "Mario & Luigi"]
let rendering = try template.render(data)

Section Tags

A Section tag {{#value}}...{{/value}} is a common syntax for three different usages:

Those behaviors are triggered by the value associated with value:

Falsey values

If the value is falsey, the section is not rendered. Falsey values are:

For example:

let template = try Template(string: "<{{#value}}Truthy{{/value}}>")

// "<Truthy>"
try template.render(["value": true])
// "<>"
try template.render([:])                  // missing value
try template.render(["value": false])     // false boolean

Collections

If the value is a collection (an array or a set), the section is rendered as many times as there are elements in the collection, and inner tags have direct access to the keys of elements:

Template:

{{# friends }}
- {{ name }}
{{/ friends }}

Data:

[
  "friends": [
    [ "name": "Hulk Hogan" ],
    [ "name": "Albert Einstein" ],
    [ "name": "Tom Selleck" ],
  ]
]

Rendering:

- Hulk Hogan
- Albert Einstein
- Tom Selleck

Other Values

If the value is not falsey, and not a collection, then the section is rendered once, and inner tags have direct access to the value's keys:

Template:

{{# user }}
- {{ name }}
- {{ score }}
{{/ user }}

Data:

[
  "user": [
    "name": "Mario"
    "score": 1500
  ]
]

Rendering:

- Mario
- 1500

Inverted Section Tags

An Inverted section tag {{^value}}...{{/value}} renders when a regular section {{#value}}...{{/value}} would not. You can think of it as the Mustache "else" or "unless".

Template:

{{# persons }}
- {{name}} is {{#alive}}alive{{/alive}}{{^alive}}dead{{/alive}}.
{{/ persons }}
{{^ persons }}
Nobody
{{/ persons }}

Data:

[
  "persons": []
]

Rendering:

Nobody

Data:

[
  "persons": [
    ["name": "Errol Flynn", "alive": false],
    ["name": "Sacha Baron Cohen", "alive": true]
  ]
]

Rendering:

- Errol Flynn is dead.
- Sacha Baron Cohen is alive.

Partial Tags

A Partial tag {{> partial }} includes another template, identified by its name. The included template has access to the currently available data:

document.mustache:

Guests:
{{# guests }}
  {{> person }}
{{/ guests }}

person.mustache:

{{ name }}

Data:

[
  "guests": [
    ["name": "Frank Zappa"],
    ["name": "Lionel Richie"]
  ]
]

Rendering:

Guests:
- Frank Zappa
- Lionel Richie

Recursive partials are supported, but your data should avoid infinite loops.

Partial lookup depends on the origin of the main template:

File System

Partial names are relative paths when the template comes from the file system (via paths or URLs):

// Load /path/document.mustache
let template = Template(path: "/path/document.mustache")

// {{> partial }} includes /path/partial.mustache.
// {{> shared/partial }} includes /path/shared/partial.mustache.

Partials have the same file extension as the main template.

// Loads /path/document.html
let template = Template(path: "/path/document.html")

// {{> partial }} includes /path/partial.html.

When your templates are stored in a hierarchy of directories, you can use absolute paths to partials, with a leading slash. For that, you need a template repository which will define the root of absolute partial paths:

let repository = TemplateRepository(directoryPath: "/path")
let template = repository.template(named: ...)

// {{> /shared/partial }} includes /path/shared/partial.mustache.

Bundle Resources

Partial names are interpreted as resource names when the template is a bundle resource:

// Load the document.mustache resource from the main bundle
let template = Template(named: "document")

// {{> partial }} includes the partial.mustache resource.

Partials have the same file extension as the main template.

// Load the document.html resource from the main bundle
let template = Template(named: "document", templateExtension: "html")

// {{> partial }} includes the partial.html resource.

General case

Generally speaking, partial names are always interpreted by a Template Repository:

Check TemplateRepository.swift for more information (read on cocoadocs.org).

Dynamic Partials

A tag {{> partial }} includes a template, the one that is named "partial". One can say it is statically determined, since that partial has already been loaded before the template is rendered:

let repo = TemplateRepository(bundle: Bundle.main)
let template = try repo.template(string: "{{#user}}{{>partial}}{{/user}}")

// Now the `partial.mustache` resource has been loaded. It will be used when
// the template is rendered. Nothing can change that.

You can also include dynamic partials. To do so, use a regular variable tag {{ partial }}, and provide the template of your choice for the key "partial" in your rendered data:

// A template that delegates the rendering of a user to a partial.
// No partial has been loaded yet.
let template = try Template(string: "{{#user}}{{partial}}{{/user}}")

// The user
let user = ["firstName": "Georges", "lastName": "Brassens", "occupation": "Singer"]

// Two different partials:
let partial1 = try Template(string: "{{firstName}} {{lastName}}")
let partial2 = try Template(string: "{{occupation}}")

// Two different renderings of the same template:
// "Georges Brassens"
try template.render(["user": user, "partial": partial1])
// "Singer"
try template.render(["user": user, "partial": partial2])

Partial Override Tags

GRMustache.swift supports Template Inheritance, like hogan.js, mustache.java and mustache.php.

A Partial Override Tag {{< layout }}...{{/ layout }} includes another template inside the rendered template, just like a regular partial tag {{> partial}}.

However, this time, the included template can contain blocks, and the rendered template can override them. Blocks look like sections, but use a dollar sign: {{$ overrideMe }}...{{/ overrideMe }}.

The included template layout.mustache below has title and content blocks that the rendered template can override:

<html>
<head>
    <title>{{$ title }}Default title{{/ title }}</title>
</head>
<body>
    <h1>{{$ title }}Default title{{/ title }}</h1>
    {{$ content }}
        Default content
    {{/ content }}}
</body>
</html>

The rendered template article.mustache:

{{< layout }}

    {{$ title }}{{ article.title }}{{/ title }}
    
    {{$ content }}
        {{{ article.html_body }}}
        <p>by {{ article.author }}</p>
    {{/ content }}
    
{{/ layout }}
let template = try Template(named: "article")
let data = [
    "article": [
        "title": "The 10 most amazing handlebars",
        "html_body": "<p>...</p>",
        "author": "John Doe"
    ]
]
let rendering = try template.render(data)

The rendering is a full HTML page:

<html>
<head>
    <title>The 10 most amazing handlebars</title>
</head>
<body>
    <h1>The 10 most amazing handlebars</h1>
    <p>...</p>
    <p>by John Doe</p>
</body>
</html>

A few things to know:

Dynamic Partial Overrides

Like a regular partial tag, a partial override tag {{< layout }}...{{/ layout }} includes a statically determined template, the very one that is named "layout".

To override a dynamic partial, use a regular section tag {{# layout }}...{{/ layout }}, and provide the template of your choice for the key "layout" in your rendered data.

Set Delimiters Tags

Mustache tags are generally enclosed by "mustaches" {{ and }}. A Set Delimiters Tag can change that, right inside a template.

Default tags: {{ name }}
{{=<% %>=}}
ERB-styled tags: <% name %>
<%={{ }}=%>
Default tags again: {{ name }}

There are also APIs for setting those delimiters. Check Configuration.tagDelimiterPair in Configuration.swift (read on cocoadocs.org).

Comment Tags

{{! Comment tags }} are simply not rendered at all.

Pragma Tags

Several Mustache implementations use Pragma tags. They start with a percent % and are not rendered at all. Instead, they trigger implementation-specific features.

GRMustache.swift interprets two pragma tags that set the content type of the template:

HTML templates is the default. They HTML-escape values rendered by variable tags {{name}}.

In a text template, there is no HTML-escaping. Both {{name}} and {{{name}}} have the same rendering. Text templates are globally HTML-escaped when included in HTML templates.

For a more complete discussion, see the documentation of Configuration.contentType in Configuration.swift.

The Context Stack and Expressions

The Context Stack

Variable and section tags fetch values in the data you feed your templates with: {{name}} looks for the key "name" in your input data, or, more precisely, in the context stack.

That context stack grows as the rendering engine enters sections, and shrinks when it leaves. Its top value, pushed by the last entered section, is where a {{name}} tag starts looking for the "name" identifier. If this top value does not provide the key, the tag digs further down the stack, until it finds the name it looks for.

For example, given the template:

{{#family}}
- {{firstName}} {{lastName}}
{{/family}}

Data:

[
    "lastName": "Johnson",
    "family": [
        ["firstName": "Peter"],
        ["firstName": "Barbara"],
        ["firstName": "Emily", "lastName": "Scott"],
    ]
]

The rendering is:

- Peter Johnson
- Barbara Johnson
- Emily Scott

The context stack is usually initialized with the data you render your template with:

// The rendering starts with a context stack containing `data`
template.render(data)

Precisely speaking, a template has a base context stack on top of which the rendered data is added. This base context is always available whatever the rendered data. For example:

// The base context contains `baseData`
template.extendBaseContext(baseData)

// The rendering starts with a context stack containing `baseData` and `data`
template.render(data)

The base context is usually a good place to register filters:

template.extendBaseContext(["each": StandardLibrary.each])

But you will generally register filters with the register(:forKey:) method, because it prevents the rendered data from overriding the name of the filter:

template.register(StandardLibrary.each, forKey: "each")

See Template for more information on the base context.

Expressions

Variable and section tags contain Expressions. name is an expression, but also article.title, and format(article.modificationDate). When a tag renders, it evaluates its expression, and renders the result.

There are four kinds of expressions:

Values

Templates render values:

template.render(["name": "Luigi"])
template.render(Person(name: "Luigi"))

You can feed templates with:

Standard Swift Types Reference

GRMustache.swift comes with built-in support for the following standard Swift types:

Bool

Numeric Types

GRMustache supports Int, UInt, Int64, UInt64, Float, Double and CGFloat:

The Swift types Int8, UInt8, etc. have no built-in support: turn them into one of the three general types before injecting them into templates.

To format numbers, you can use NumberFormatter:

let percentFormatter = NumberFormatter()
percentFormatter.numberStyle = .percent

let template = try Template(string: "{{ percent(x) }}")
template.register(percentFormatter, forKey: "percent")

// Rendering: 50%
let data = ["x": 0.5]
let rendering = try template.render(data)

More info on Formatter.

String

Exposed keys:

Set

Exposed keys:

Array

Exposed keys:

In order to render array indexes, or vary the rendering according to the position of elements in the array, use the each filter from the Standard Library:

document.mustache:

Users with their positions:
{{# each(users) }}
- {{ @indexPlusOne }}: {{ name }}
{{/}}

Comma-separated user names:
{{# each(users) }}{{ name }}{{^ @last }}, {{/}}{{/}}.
let template = try! Template(named: "document")

// Register StandardLibrary.each for the key "each":
template.register(StandardLibrary.each, forKey: "each")

// Users with their positions:
// - 1: Alice
// - 2: Bob
// - 3: Craig
// 
// Comma-separated user names: Alice, Bob, Craig.
let users = [["name": "Alice"], ["name": "Bob"], ["name": "Craig"]]
let rendering = try! template.render(["users": users])

Dictionary

In order to iterate over the key/value pairs of a dictionary, use the each filter from the Standard Library:

document.mustache:

{{# each(dictionary) }}
    key: {{ @key }}, value: {{.}}
{{/}}
let template = try! Template(named: "document")

// Register StandardLibrary.each for the key "each":
template.register(StandardLibrary.each, forKey: "each")

// Renders "key: name, value: Freddy Mercury"
let dictionary = ["name": "Freddy Mercury"]
let rendering = try! template.render(["dictionary": dictionary])

NSObject

The rendering of NSObject depends on the actual class:

Custom Types

NSObject subclasses

NSObject subclasses can trivially feed your templates:

// An NSObject subclass
class Person : NSObject {
    let name: String
    
    init(name: String) {
        self.name = name
    }
}

// Charlie Chaplin has a mustache.
let person = Person(name: "Charlie Chaplin")
let template = try Template(string: "{{name}} has a mustache.")
let rendering = try template.render(person)

When extracting values from your NSObject subclasses, GRMustache.swift uses the Key-Value Coding method valueForKey:, as long as the key is "safe" (safe keys are the names of declared properties, including NSManagedObject attributes).

Subclasses can alter this default behavior by overriding the mustacheBox method of the MustacheBoxable protocol, described below:

Pure Swift Values and MustacheBoxable

Key-Value Coding is not available for Swift enums, structs and classes, regardless of eventual @objc or dynamic modifiers. Swift values can still feed templates, though, with a little help.

// Define a pure Swift object:
struct Person {
    let name: String
}

To let Mustache templates extract the name key out of a person so that they can render {{ name }} tags, we need to explicitly help the Mustache engine by conforming to the MustacheBoxable protocol:

extension Person : MustacheBoxable {
    
    // Feed templates with a dictionary:
    var mustacheBox: MustacheBox {
        return Box(["name": self.name])
    }
}

Your mustacheBox implementation will generally call the Box function on a regular value that itself adopts the MustacheBoxable protocol (such as String or Int), or an array, a set, or a dictionary.

Now we can render persons, arrays of persons, dictionaries of persons, etc:

// Freddy Mercury has a mustache.
let person = Person(name: "Freddy Mercury")
let template = try Template(string: "{{name}} has a mustache.")
let rendering = try template.render(person)

Boxing a dictionary is an easy way to build a box. However there are many kinds of boxes: check the rest of this documentation.

Lambdas

Mustache lambdas are functions that let you perform custom rendering. There are two kinds of lambdas: those that process section tags, and those that render variable tags.

// `{{fullName}}` renders just as `{{firstName}} {{lastName}}.`
let fullName = Lambda { "{{firstName}} {{lastName}}" }

// `{{#wrapped}}...{{/wrapped}}` renders the content of the section, wrapped in
// a <b> HTML tag.
let wrapped = Lambda { (string) in "<b>\(string)</b>" }

// <b>Frank Zappa is awesome.</b>
let templateString = "{{#wrapped}}{{fullName}} is awesome.{{/wrapped}}"
let template = try Template(string: templateString)
let data: [String: Any] = [
    "firstName": "Frank",
    "lastName": "Zappa",
    "fullName": fullName,
    "wrapped": wrapped]
let rendering = try template.render(data)

Lambdas are a special case of custom rendering functions. The raw RenderFunction type gives you extra flexibility when you need to perform custom rendering. See CoreFunctions.swift (read on cocoadocs.org).

:point_up: Note: Mustache lambdas slightly overlap with dynamic partials. Lambdas are required by the Mustache specification. Dynamic partials are more efficient because they avoid parsing lambda strings over and over.

Filters

Filters apply like functions, with parentheses: {{ uppercase(name) }}.

Generally speaking, using filters is a three-step process:

// 1. Define the filter using the `Filter()` function:
let uppercase = Filter(...)

// 2. Assign a name to your filter, and register it in a template:
template.register(uppercase, forKey: "uppercase")

// 3. Render
template.render(...)

It helps thinking about four kinds of filters:

Value Filters

Value filters transform any type of input. They can return anything as well.

For example, here is a square filter which squares integers:

// Define the `square` filter.
//
// square(n) evaluates to the square of the provided integer.
let square = Filter { (n: Int?) in
    guard let n = n else {
        // No value, or not an integer: return nil.
        // We could throw an error as well.
        return nil
    }
    
    // Return the result
    return n * n
}

// Register the square filter in our template:
let template = try Template(string: "{{n}} × {{n}} = {{square(n)}}")
template.register(square, forKey:"square")

// 10 × 10 = 100
let rendering = try template.render(["n": 10])

Filters can accept a precisely typed argument as above. You may prefer managing the value type yourself:

// Define the `abs` filter.
//
// abs(x) evaluates to the absolute value of x (Int or Double):
let absFilter = Filter { (box: MustacheBox) in
    switch box.value {
    case let int as Int:
        return abs(int)
    case let double as Double:
        return abs(double)
    default:
        return nil
    }
}

You can process collections and dictionaries as well, and return new ones:

// Define the `oneEveryTwoItems` filter.
//
// oneEveryTwoItems(collection) returns the array of even items in the input
// collection.
let oneEveryTwoItems = Filter { (box: MustacheBox) in
    // `box.arrayValue` returns a `[MustacheBox]` for all boxed collections
    // (Array, Set, NSArray, etc.).
    guard let boxes = box.arrayValue else {
        // No value, or not a collection: return the empty box
        return nil
    }
    
    // Rebuild another array with even indexes:
    var result: [MustacheBox] = []
    for (index, box) in boxes.enumerated() where index % 2 == 0 {
        result.append(box)
    }
    
    return result
}

// A template where the filter is used in a section, so that the items in the
// filtered array are iterated:
let templateString = "{{# oneEveryTwoItems(items) }}<{{.}}>{{/ oneEveryTwoItems(items) }}"
let template = try Template(string: templateString)

// Register the oneEveryTwoItems filter in our template:
template.register(oneEveryTwoItems, forKey: "oneEveryTwoItems")

// <1><3><5><7><9>
let rendering = try template.render(["items": Array(1..<10)])

Multi-arguments filters are OK as well. but you use the VariadicFilter() function, this time:

// Define the `sum` filter.
//
// sum(x, ...) evaluates to the sum of provided integers
let sum = VariadicFilter { (boxes: [MustacheBox]) in
    var sum = 0
    for box in boxes {
        sum += (box.value as? Int) ?? 0
    }
    return sum
}

// Register the sum filter in our template:
let template = try Template(string: "{{a}} + {{b}} + {{c}} = {{ sum(a,b,c) }}")
template.register(sum, forKey: "sum")

// 1 + 2 + 3 = 6
let rendering = try template.render(["a": 1, "b": 2, "c": 3])

Filters can chain and generally be part of more complex expressions:

Circle area is {{ format(product(PI, circle.radius, circle.radius)) }} cm².

When you want to format values, just use NumberFormatter, DateFormatter, or generally any Foundation's Formatter. They are ready-made filters:

let percentFormatter = NumberFormatter()
percentFormatter.numberStyle = .percent

let template = try Template(string: "{{ percent(x) }}")
template.register(percentFormatter, forKey: "percent")

// Rendering: 50%
let data = ["x": 0.5]
let rendering = try template.render(data)

More info on formatters.

Pre-Rendering Filters

Value filters as seen above process input values, which may be of any type (bools, ints, collections, etc.). Pre-rendering filters always process strings, whatever the input value. They have the opportunity to alter those strings before they get actually included in the final template rendering.

You can, for example, reverse a rendering:

// Define the `reverse` filter.
//
// reverse(x) renders the reversed rendering of its argument:
let reverse = Filter { (rendering: Rendering) in
    let reversedString = String(rendering.string.characters.reversed())
    return Rendering(reversedString, rendering.contentType)
}

// Register the reverse filter in our template:
let template = try Template(string: "{{reverse(value)}}")
template.register(reverse, forKey: "reverse")

// ohcuorG
try template.render(["value": "Groucho"])

// 321
try template.render(["value": 123])

Such filter does not quite process a raw string, as you have seen. It processes a Rendering, which is a flavored string, a string with its contentType (text or HTML).

This rendering will usually be text: simple values (ints, strings, etc.) render as text. Our reversing filter preserves this content-type, and does not mangle HTML entities:

// &gt;lmth&lt;
try template.render(["value": "<html>"])

Custom Rendering Filters

An example will show how they can be used:

// Define the `pluralize` filter.
//
// {{# pluralize(count) }}...{{/ }} renders the plural form of the
// section content if the `count` argument is greater than 1.
let pluralize = Filter { (count: Int?, info: RenderingInfo) in
    
    // The inner content of the section tag:
    var string = info.tag.innerTemplateString
    
    // Pluralize if needed:
    if let count = count, count > 1 {
        string += "s"  // naive
    }
    
    return Rendering(string)
}

// Register the pluralize filter in our template:
let templateString = "I have {{ cats.count }} {{# pluralize(cats.count) }}cat{{/ }}."
let template = try Template(string: templateString)
template.register(pluralize, forKey: "pluralize")

// I have 3 cats.
let data = ["cats": ["Kitty", "Pussy", "Melba"]]
let rendering = try template.render(data)

As those filters perform custom rendering, they are based on RenderFunction, just like lambdas. Check the RenderFunction type in CoreFunctions.swift for more information about the RenderingInfo and Rendering types (read on cocoadocs.org).

Advanced Filters

All the filters seen above are particular cases of FilterFunction. "Value filters", "Pre-rendering filters" and "Custom rendering filters" are common use cases that are granted with specific APIs.

Yet the library ships with a few built-in filters that don't quite fit any of those categories. Go check their documentation. And since they are all written with public GRMustache.swift APIs, check also their source code, for inspiration. The general FilterFunction itself is detailed in CoreFunctions.swift (read on cocoadocs.org).

Advanced Boxes

Values that feed templates are able of many different behaviors. Let's review some of them:

This variety of behaviors is made possible by the MustacheBox type. Whenever a value, array, filter, etc. feeds a template, it is turned into a box that interact with the rendering engine.

Let's describe in detail the rendering of the {{ F(A) }} tag, and shed some light on the available customizations:

  1. The A and F expressions are evaluated: the rendering engine looks in the context stack for boxes that return a non-empty box for the keys "A" and "F". The key-extraction service is provided by a customizable KeyedSubscriptFunction.

    This is how NSObject exposes its properties, and Dictionary, its keys.

  2. The customizable FilterFunction of the F box is evaluated with the A box as an argument.

    The Result box may well depend on the customizable value of the A box, but all other facets of the A box may be involved. This is why there are various types of filters.

  3. The rendering engine then looks in the context stack for all boxes that have a customized WillRenderFunction. Those functions have an opportunity to process the Result box, and eventually return another one.

    This is how, for example, a boxed DateFormatter can format all dates in a section: its WillRenderFunction formats dates into strings.

  4. The resulting box is ready to be rendered. For regular and inverted section tags, the rendering engine queries the customizable boolean value of the box, so that {{# F(A) }}...{{/}} and {{^ F(A) }}...{{/}} can't be both rendered.

    The Bool type obviously has a boolean value, but so does String, so that empty strings are considered falsey.

  5. The resulting box gets eventually rendered: its customizable RenderFunction is executed. Its Rendering result is HTML-escaped, depending on its content type, and appended to the final template rendering.

    Lambdas use such a RenderFunction, so do pre-rendering filters and custom rendering filters.

  6. Finally the rendering engine looks in the context stack for all boxes that have a customized DidRenderFunction.

    This one is used by Localizer and Logger goodies.

All those customizable properties are exposed in the low-level MustacheBox initializer:

// MustacheBox initializer
init(
    value value: Any? = nil,
    boolValue: Bool? = nil,
    keyedSubscript: KeyedSubscriptFunction? = nil,
    filter: FilterFunction? = nil,
    render: RenderFunction? = nil,
    willRender: WillRenderFunction? = nil,
    didRender: DidRenderFunction? = nil)

We'll below describe each of them individually, even though you can provide several ones at the same time:

By mixing all those parameters, you can finely tune the behavior of a box.

Built-in goodies

The library ships with built-in goodies that will help you render your templates: format values, render array indexes, localize templates, etc.