valueForKey:
, description
, <NSFastEnumeration>
.
Those are the only interfaces that you have to care about when providing data to GRMustache.
-
valueForKey:
is the standard Key-Value Coding method, that GRMustache invokes when looking for the data that will be rendered. Basically, for a{{name}}
tag to be rendered, all you need to provide is an NSDictionary with the@"name"
key, or an object declaring thename
property. -
description
is the standard NSObject method, that GRMustache invokes when rendering the data it has fetched fromvalueForKey:
. Most classes of Apple frameworks already have sensible implementations ofdescription
: NSString, NSNumber, etc. You generally won't have to think a lot about it. -
NSFastEnumeration
is the standard protocol for enumerable objects. The most obvious enumerable is NSArray. There are others, and you may provide your own. Objects that conform to theNSFastEnumeration
protocol are the base of GRMustache loops. You'll read more on this topic in the loops.md guide.
For instance, let's consider the following code:
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"Arthur" forKey:@"name"];
NSString *templateString = @"Hello {{name}}!";
NSString *rendering = [GRMustacheTemplate renderObject:dictionary fromString:templateString error:NULL];
- When GRMustache renders the
{{name}}
tag, it invokesvalueForKey:
with the key@"name"
on the dictionary. It gets@"Arthur"
as a result. - The
description
method of NSString returns the string itself:@"Arthur"
. - Hence we'll get the expected rendering:
@"Hello Arthur!"
.
Mustache does a little more than rendering plain {{name}}
tags. Let's review Mustache features and how GRMustache help you leverage them.
-
This guide digs into Mustache sections such as
{{#section}}...{{/section}}
, and the key lookup mechanism. -
Learn how to render template sections as many times as there are objects in enumerable objects such as arrays.
-
Control whether a Mustache section should render or not.
-
Mustache has "lambda sections". These are sections that allow you to execute custom code, and implement nifty features like caching, filtering, whatever, on portions of your templates.