Contents

AlwaysRightInstitute/Mustache

A simple Mustache parser/evaluator for Swift

Example

Sample Mustache:

Hello {{name}} You have just won {{& value}} dollars! {{#in_ca}} Well, {{{taxed_value}}} dollars, after taxes. {{/in_ca}} {{#addresses}} Has address in: {{city}} {{/addresses}} {{^addresses}} Has NO addresses {{/addresses}}

The template features value access: {{name}}, conditionals: {{#in_ca}}, as well as repetitions: {{#addresses}}.

Sample code to parse and evaluate the template:

let sampleDict : [ String : Any ] = [ "name" : "Chris", "value" : 10000, "taxed_value" : Int(10000 - (10000 * 0.4)), "in_ca" : true, "addresses" : [ [ "city" : "Cupertino" ] ] ]

var parser = MustacheParser() let tree = parser.parse(string: template) let result = tree.render(object: sampleDict)

You get the idea.

Swift 5 Dynamic Callable

In Swift 5 you can expose Mustache templates as regular Swift functions.

To declare a Mustache backed function:

let generateHTMLForWinner = Mustache(
    """
    {% raw %}Hello {{name}}
    You have just won {{& value}} dollars!
    {{#in_ca}}
        Well, {{{taxed_value}}} dollars, after taxes.
    {{/in_ca}}
    {{#addresses}}
        Has address in: {{city}}
    {{/addresses}}
    {{^addresses}}
        Has NO addresses
    {{/addresses}}{% endraw %}
    """
)

To call the function:

let winners = [
    generateHTMLForWinner(
        name: "Chris", value: 10000,
        taxed_value: 6000, in_ca: true,
        addresses: [[ "city": "Cupertino" ]]
    ),
    generateHTMLForWinner(
        name: "Michael", value: 6000,
        taxed_value: 6000, in_ca: false,
        addresses: [[ "city": "Austin" ]]
    )
]

Checkout our blog for more info on this.

Who

mustache is brought to you by The Always Right Institute and ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of. We don't like people who are wrong.

Ask questions on the Noze.io Slack.

Package Metadata

Repository: AlwaysRightInstitute/Mustache

Stars: 24

Forks: 6

Open issues: 1

Default branch: develop

Primary language: swift

License: Apache-2.0

Topics: mustache, swift, swift-library, swift3

README: README.md