GRMustache provides built-in support for NSFormatter and its subclasses such as NSNumberFormatter and NSDateFormatter.
There are ready-made filters and rendering objects (aka "lambdas").
Just add your formatters to the data you render: they get ready to be used as filters:
Document.mustache
:
{{ percent(x) }}
Rendering code:
NSNumberFormatter *percentFormatter = [NSNumberFormatter new];
percentFormatter.numberStyle = NSNumberFormatterPercentStyle;
id data = @{
@"x": @(0.5),
@"percent": percentFormatter,
};
NSString *rendering = [GRMustacheTemplate renderObject:data
fromResource:@"Document"
bundle:nil
error:NULL];
Rendering:
50%
Rendering objects are able to render a full Mustache section as they want. NSFormatters take the opportunity to format all variable tags inside the section:
Document.mustache
:
{{# percent }}
hourly: {{ hourly }}
daily: {{ daily }}
weekly: {{ weekly }}
{{/ percent }}
Rendering code:
NSNumberFormatter *percentFormatter = [NSNumberFormatter new];
percentFormatter.numberStyle = NSNumberFormatterPercentStyle;
id data = @{
@"hourly": @(0.1),
@"daily": @(1.5),
@"weekly": @(4),
@"percent": percentFormatter,
};
NSString *rendering = [GRMustacheTemplate renderObject:data
fromResource:@"Document"
bundle:nil
error:NULL];
Rendering:
hourly: 10%
daily: 150%
weekly: 400%
Variable tags buried inside inner sections are escaped as well, so that you can render loop and conditional sections. However, values that can't be formatted are left untouched:
Document.mustache
:
{{# percent }}
{{# ingredients }}
- {{ name }}: {{ proportion }} {{! name is intact, proportion is formatted. }}
{{/ ingredients }}
{{/ percent }}
Would render:
- bread: 50%
- ham: 22%
- butter: 43%
Precisely speaking, "values that can't be formatted" are the ones that have the stringForObjectValue:
method return nil, as stated by apple.
Typically, NSNumberFormatter only formats numbers, and NSDateFormatter, dates: you can safely mix various data types in a section controlled by those well-behaved formatters.
NSFormatter has been turned into a citizen of GRMustache using public APIs: check the code for inspiration.