Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

131 lines (77 sloc) 5.568 kB

up, next

GRMustache introduction

Make sure you get familiar with the Mustache syntax and features first:


Core Mustache

  • variable tags, as {{name}}, {{{name}}} and {{&name}} (HTML-escaped or not)
  • section tags (boolean, loop, lambda, inverted), as {{#name}}...{{/name}} and {{^name}}...{{/name}}
  • partial tags, as {{> partial}}
  • comment tag, as {{! comment }}
  • "set delimiter tags", as {{=<% %>=}}

Overlooked Mustache

Those features are not documented in mustache.5.html, despite their inclusion in the Mustache specification:

  • Key paths, as {{ }}, for direct access to an object's property.
  • "Implicit iterator", aka {{.}}, directly renders the current object (useful when looping over strings, for instance).
  • "Mustache lambdas", allow tags such as {{name}} and {{#name}}...{{/name}} to perform custom rendering. Those are documented at the Rendering Objects Guide.

Mustache Language Extensions

Genuine Mustache falls short on a few topics. GRMustache implements syntaxes that are not in the specification:

  • Empty closing tags, as in {{#name}}...{{/}}

    You don't have to repeat the opening expression in the closing tag.

  • "Else", as in {{#name}}...{{^name}}...{{/name}}

    You don't have to close a regular section if it is immediately followed by its inverted form.

    The short form {{#name}}...{{^}}...{{/}} is accepted, as well as the "unless" form {{^name}}...{{#}}...{{/}}.

  • "Anchored key paths", as {{ .name }} which prevents the lookup of the name key in the context stack built by Mustache sections, and guarantees that the name key will be fetched from the very current context.

    If you are not familiar with the "context stack" and the Mustache key lookup mechanism, check the Runtime Guide.

  • Loops in variable tags: a simple variable tag {{items}} renders as the concatenation of the rendering of each individual items. You may think of Ruby on Rails' <%= render @items %>: check the Rendering Objects Guide.

  • "Filters", as {{ uppercase(name) }}.

    Filters are documented in the Filters Guide.

  • Support for partial templates in a file system hierarchy.

    Use relative {{> header }} or absolute paths {{> /shared/header }} to your partial templates: see the Partials Guide.

  • "Overridable partials", aka "template inheritance", inspired by hogan.js and spullara/

    Overridable partials are documented in the Partials Guide.

Powerful APIs

GRMustache is a Mustache engine that you can hack in.

Should you eventually build a library of reusable code snippets, you'll find Protected Contexts useful.

Getting started

Rendering dictionaries from template strings

You'll generally gather a template and a data object that will fill the "holes" in the template.

The shortest way to render a template is to mix a literal template string and a dictionary:

#import "GRMustache.h"

// Render "Hello Arthur!"
NSString *rendering = [GRMustacheTemplate renderObject:@{ @"name": @"Arthur" }
                                            fromString:@"Hello {{name}}!"

+[GRMustacheTemplate renderObject:fromString:error:] is documented in the Templates Guide.

Rendering model objects from template resources

However, your templates will often be stored as resources in your application bundle, and your data will come from your model objects. It turns out the following code should be more common:

#import "GRMustache.h"

// Render a profile document from the `Profile.mustache` resource:
Person *person = [Person personWithName:@"Arthur"];
NSString *profile = [GRMustacheTemplate renderObject:person

+[GRMustacheTemplate renderObject:fromResource:bundle:error:] is documented in the Templates Guide.

Reusing templates

You will spare CPU cycles by creating and reusing template objects:

#import "GRMustache.h"

GRMustacheTemplate *template = [GRMustacheTemplate templateFromResource:@"Profile" bundle:nil error:NULL];

// Render ad nauseam
NSString *arthurProfile = [template renderObject:arthur error:NULL];
NSString *barbaraProfile = [template renderObject:barbara error:NULL];

+[GRMustacheTemplate templateFromResource:bundle:error:] and -[GRMustacheTemplate renderObject:error:] are documented in the Templates Guide.

Other use cases

Examples above are common use cases for MacOS and iOS applications. The library does much more.

up, next

Jump to Line
Something went wrong with that request. Please try again.