Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (79 sloc) 5.7 KB

up, next

GRMustache introduction


Core Mustache

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

  • 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 both {{name}} and {{#name}}...{{/name}} tags to invoke your own rendering code. This is documented in the Rendering Objects Guide.

Core Engine Extensions

Genuine Mustache falls short on a few topics. GRMustache core engine implements syntaxes and features 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 enforces lookup of the name key in the immediate context instead of going through the context stack built by Mustache sections.

    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 a concatenation of the rendering of each individual item. 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/, allow you to define reusable template layouts.

    Overridable partials are documented in the Partials Guide.

  • Text templates.

    The Mustache language focuses on rendering HTML, and safely HTML-escape your data. GRMustache also support text templates, that do not escape anything. Check the HTML vs. Text Templates Guide.


GRMustache's core engine is extensible. Feel free to hook 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.