Inherits from NSObject
Conforms to GRMustacheRendering
Declared in GRMustacheTemplate.h

Overview

The GRMustacheTemplate class provides with Mustache template rendering services.

Companion guide: https://github.com/groue/GRMustache/blob/master/Guides/templates.md

Properties

baseContext

The template’s base context: all rendering start from this context.

@property (nonatomic, retain) GRMustacheContext *baseContext

Availability

v6.0

Discussion

Its default value comes from the configuration of the source template repository. Unless specified, it contains the GRMustache standard library.

Declared In

GRMustacheTemplate.h

templateRepository

Returns the template repository that issued the receiver.

@property (nonatomic, retain, readonly) GRMustacheTemplateRepository *templateRepository

Availability

v7.0

Discussion

All templates belong a template repository:

Declared In

GRMustacheTemplate.h

Class Methods

renderObject:fromResource:bundle:error:

Renders an object from a bundle resource template.

+ (NSString *)renderObject:(id)object fromResource:(NSString *)name bundle:(NSBundle *)bundle error:(NSError **)error

Parameters

object

An object used for interpreting Mustache tags.

name

The name of a bundle resource of extension “mustache”.

bundle

The bundle where to look for the template resource. If nil, the main bundle is used.

error

If there is an error during rendering, upon return contains an NSError object that describes the problem.

Return Value

A string containing the rendered template.

Availability

v1.0

Discussion

If you provide nil as a bundle, the resource will be looked in the main bundle, with a “mustache” extension.

The template resource must be encoded in UTF8. See the GRMustacheTemplateRepository class for more encoding options.

Declared In

GRMustacheTemplate.h

renderObject:fromString:error:

Renders an object from a template string.

+ (NSString *)renderObject:(id)object fromString:(NSString *)templateString error:(NSError **)error

Parameters

object

An object used for interpreting Mustache tags.

templateString

The template string.

error

If there is an error during rendering, upon return contains an NSError object that describes the problem.

Return Value

A string containing the rendered template.

Availability

v1.0

Declared In

GRMustacheTemplate.h

templateFromContentsOfFile:error:

Parses a template file, and returns a compiled template.

+ (instancetype)templateFromContentsOfFile:(NSString *)path error:(NSError **)error

Parameters

path

The path of the template.

error

If there is an error loading or parsing template and partials, upon return contains an NSError object that describes the problem.

Return Value

A GRMustacheTemplate instance.

Availability

v1.11

Discussion

The template at path must be encoded in UTF8. See the GRMustacheTemplateRepository class for more encoding options.

Declared In

GRMustacheTemplate.h

templateFromContentsOfURL:error:

Parses a template file, and returns a compiled template.

+ (instancetype)templateFromContentsOfURL:(NSURL *)url error:(NSError **)error

Parameters

url

The URL of the template.

error

If there is an error loading or parsing template and partials, upon return contains an NSError object that describes the problem.

Return Value

A GRMustacheTemplate instance.

Availability

v1.11

Discussion

The template at url must be encoded in UTF8. See the GRMustacheTemplateRepository class for more encoding options.

Declared In

GRMustacheTemplate.h

templateFromResource:bundle:error:

Parses a bundle resource template, and returns a compiled template.

+ (instancetype)templateFromResource:(NSString *)name bundle:(NSBundle *)bundle error:(NSError **)error

Parameters

name

The name of a bundle resource of extension “mustache”.

bundle

The bundle where to look for the template resource. If nil, the main bundle is used.

error

If there is an error loading or parsing template and partials, upon return contains an NSError object that describes the problem.

Return Value

A GRMustacheTemplate instance.

Availability

v1.11

Discussion

If you provide nil as a bundle, the resource will be looked in the main bundle.

The template resource must be encoded in UTF8. See the GRMustacheTemplateRepository class for more encoding options.

Declared In

GRMustacheTemplate.h

templateFromString:error:

Parses a template string, and returns a compiled template.

+ (instancetype)templateFromString:(NSString *)templateString error:(NSError **)error

Parameters

templateString

The template string.

error

If there is an error loading or parsing template and partials, upon return contains an NSError object that describes the problem.

Return Value

A GRMustacheTemplate instance.

Availability

v1.11

Declared In

GRMustacheTemplate.h

Instance Methods

extendBaseContextWithObject:

Extends the base context of the receiver with the provided object, making its keys available for all renderings.

- (void)extendBaseContextWithObject:(id)object

Parameters

object

An object

Availability

v6.8

Discussion

For example:

GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:@"{{name}}" error:NULL];

// Have the `name` key defined for all renderings of the template:
id object = @{ @"name": @"Arthur" };
[template extendBaseContextWithObject:object];

// Renders "Arthur"

[template renderObject:nil error:NULL];

Keys defined by object can be overriden by other objects that will eventually enter the context stack:

// Renders "Billy", not "Arthur"
[template renderObject:@{ @"name": @"Billy" } error:NULL];

This method is a shortcut. It is equivalent to the following line of code:

template.baseContext = [template.baseContext contextByAddingObject:object];

Declared In

GRMustacheTemplate.h

extendBaseContextWithProtectedObject:

Extends the base context of the receiver with the provided object, making its keys available for all renderings.

- (void)extendBaseContextWithProtectedObject:(id)object

Parameters

object

An object

Availability

v6.8

Discussion

Keys defined by object are given priority, which means that they can not be overriden by other objects that will eventually enter the context stack.

For example:

GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:@"{{precious}}" error:NULL];

// The `precious` key is given priority:
id object = @{ @"precious": @"gold" };
[template extendBaseContextWithProtectedObject:object];

// Renders "gold", not "lead".
[template renderObject:@{ @"precious": @"lead" } error:NULL];

This method is a shortcut. It is equivalent to the following line of code:

template.baseContext = [template.baseContext contextByAddingProtectedObject:object];

Declared In

GRMustacheTemplate.h

extendBaseContextWithTagDelegate:

Extends the base context of the receiver with a tag delegate, making it aware of the rendering of all tags in the template.

- (void)extendBaseContextWithTagDelegate:(id<GRMustacheTagDelegate>)tagDelegate

Parameters

tagDelegate

A tag delegate

Availability

v6.8

Discussion

This method is a shortcut. It is equivalent to the following line of code:

template.baseContext = [template.baseContext contextByAddingTagDelegate:tagDelegate];

Declared In

GRMustacheTemplate.h

renderContentWithContext:HTMLSafe:error:

Returns the rendering of the receiver, given a rendering context.

- (NSString *)renderContentWithContext:(GRMustacheContext *)context HTMLSafe:(BOOL *)HTMLSafe error:(NSError **)error

Parameters

context

A rendering context.

HTMLSafe

Upon return contains YES or NO, depending on the content type of the template, as set by the configuration of the source template repository. HTML templates yield YES, text templates yield NO.

error

If there is an error rendering the tag, upon return contains an NSError object that describes the problem.

Return Value

The rendering of the template.

Availability

v6.0

Declared In

GRMustacheTemplate.h

renderObject:error:

Renders a template with a context stack initialized with the provided object on top of the base context.

- (NSString *)renderObject:(id)object error:(NSError **)error

Parameters

object

An object used for interpreting Mustache tags.

error

If there is an error rendering the template and its partials, upon return contains an NSError object that describes the problem.

Return Value

A string containing the rendered template.

Availability

v6.0

Declared In

GRMustacheTemplate.h

renderObjectsFromArray:error:

Renders a template with a context stack initialized with the provided objects on top of the base context.

- (NSString *)renderObjectsFromArray:(NSArray *)objects error:(NSError **)error

Parameters

objects

An array of context objects for interpreting Mustache tags.

error

If there is an error rendering the template and its partials, upon return contains an NSError object that describes the problem.

Return Value

A string containing the rendered template.

Availability

v6.0

Declared In

GRMustacheTemplate.h