Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

146 lines (87 sloc) 5.907 kb

up, next

GRMustache introduction

Make sure you get familiar with the Mustache syntax and features first: http://mustache.github.com/mustache.5.html.

Features

Core Mustache

  • variables, as {{name}} and {{{name}}} (HTML-escaped or not)
  • sections (boolean, loop, lambda, inverted), as {{#name}}...{{/name}} and {{^name}}...{{/name}}
  • partial templates inclusion, including recursive partials, as {{> partial}}
  • comments, 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 {{ person.name }}, for direct access to an object's property.
  • "implicit iterator", aka {{.}}, directly renders the current object (useful when looping over strings, for instance).

Language extensions

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

  • "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 key lookup mechanism, check Guides/runtime/context_stack.md.

    This extension is backed on the discussions at mustache/spec#10 and mustache/spec#52.

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

    This extension is backed on the discussion at mustache/spec#41

Template delegate

All the nice Objective-C classes you know allow for observation and customization through delegates. GRMustache will not let you down.

Getting started

Rendering dictionaries from template strings

You'll generally gather a template string 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!"

NSDictionary *person = @{ @"name": @"Arthur" };
NSString *templateString = @"Hello {{name}}!";
NSString *rendering = [GRMustacheTemplate renderObject:person fromString:templateString error:NULL];

+[GRMustacheTemplate renderObject:fromString:error:] is documented in Guides/templates.md.

Rendering model objects from template resources

However, generally speaking, your templates will 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 fromResource:@"Profile" bundle:nil error:NULL];

+[GRMustacheTemplate renderObject:fromString:error:] is documented in Guides/templates.md.

Reusing templates

Finally, should you render a single template several times, you will spare CPU cycles by using a single GRMustacheTemplate instance:

#import "GRMustache.h"

// Initialize a template from the `Profile.mustache` resource:
GRMustacheTemplate *profileTemplate = [GRMustacheTemplate templateFromResource:@"Profile" bundle:nil error:NULL];

// Render two profile documents
NSString *arthurProfile = [profileTemplate renderObject:arthur];
NSString *barbieProfile = [profileTemplate renderObject:barbie];

+[GRMustacheTemplate templateFromResource:bundle:error:] and -[GRMustacheTemplate renderObject:] are documented in Guides/templates.md.

Other use cases

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

Full documentation map

Mustache syntax

Guides

Loading templates:

Rendering templates:

Advanced Mustache:

  • helpers.md: how to process the template canvas before it is rendered with Mustache "lambda sections".
  • filters.md: how to process data before it is rendered with "filters".
  • delegate.md: how to hook into template rendering.

Sample code:

  • layout: how to wrap a template in a "layout" template
  • number_formatting.md: how to format numbers
  • indexes.md: how to render array indexes, render sections for the first or the last element, for odd or even elements, etc.
  • localization.md: how to localize portions of your templates

Reference

  • Reference: the GRMustache reference, automatically generated from inline documentation, for fun and profit, by appledoc.

Internals

  • forking.md: the forking guide tells you everything about GRMustache organization.

up, next

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