A simple Mustache parser/evaluator for Swift
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.d
Sources
Tests
mustache.xcodeproj
xcconfig
.gitignore
.swift-version
.travis.yml
GNUmakefile
LICENSE
Package.swift
Package@swift-4.swift
README.md

README.md

Mustache

Swift3 macOS tuxOS Travis

A simple Mustache parser/evaluator for Swift.

Mustache is a very simple templating language. Implementations are available for pretty much any programming language (check the Mustache website) - this is one for Swift.

In the context of Noze.io you don't need to call this manually, but you can just use the Mustache template support in the Noze.io Express module. Checkout the express-simple app as an example on how to do this.

This Mustache implementation comes with a very simple Key-Value coding implementation, which is used to extract values from model objects for rendering. With that you can render both, generic Dictionary/Array structures as well as Swift objects with properties. Since the reflection capabilities of Swift are pretty limited, so is the KVC implementation.

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" ]
  ]
]

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

You get the idea.

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.