Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleanup description of section delegates in number formatting sample …

…code
  • Loading branch information...
commit dc3c200d0f3bffde75cea55a42a837bc0a61c446 1 parent 7afeec3
@groue authored
View
5 Guides/sample_code/number_formatting.md
@@ -129,7 +129,6 @@ Here is the rendering code:
* template.
*/
- // Attach a float to the "float" key
[data setObject:[NSNumber numberWithFloat:0.5] forKey:@"float"];
@@ -146,9 +145,9 @@ But we haven't told yet how those number formatters will be used for rendering t
We'll have the NSNumberFormatter class conform to the GRMustacheTemplateDelegate protocol.
-As a consequence, GRMustache will give formatters that are attached to Mustache sections the opportunity to hook in the template rendering while rendering those sections.
+As a consequence, GRMustache will give formatters that are attached to Mustache sections the opportunity to hook in the template rendering while rendering those sections. This behavior is described in the [GRMustacheTemplateDelegate](../delegate.md) guide.
-We'll implement the `template:willInterpretReturnValueOfInvocation:as:` callback, that is able to tell GRMustache which object it should render: NSNumberFormatter instances will tell GRMustache to render formatted numbers instead of plain numbers.
+We just have to implement the `template:willInterpretReturnValueOfInvocation:as:` callback, that is able to tell GRMustache which object it should render. NSNumberFormatter instances will tell GRMustache to render formatted numbers instead of plain numbers.
Let's first have NSNumberFormatter conform to the GRMustacheTemplateDelegate protocol:
View
29 Guides/sample_code/number_formatting/GRMustacheNumberFormatting/Document.m
@@ -33,8 +33,8 @@ @implementation Document
- (NSString *)render
{
/**
- * So, our goal is to format all numbers in the `{{#PERCENT_FORMAT}}` and
- * `{{#DECIMAL_FORMAT}}` sections of template.mustache.
+ * So, our goal is to format all numbers in the `{{#formatPercent}}` and
+ * `{{#formatDecimal}}` sections of template.mustache.
*
* First, we attach a NSNumberFormatter instance to those sections. This is
* done by setting NSNumberFormatter instances to corresponding keys in the
@@ -48,15 +48,15 @@ - (NSString *)render
NSMutableDictionary *data = [NSMutableDictionary dictionary];
- // Attach a percent NSNumberFormatter to the "PERCENT_FORMAT" key
+ // Attach a percent NSNumberFormatter to the "formatPercent" key
NSNumberFormatter *percentNumberFormatter = [[NSNumberFormatter alloc] init];
percentNumberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
- [data setObject:percentNumberFormatter forKey:@"PERCENT_FORMAT"];
+ [data setObject:percentNumberFormatter forKey:@"formatPercent"];
- // Attach a decimal NSNumberFormatter to the "DECIMAL_FORMAT" key
+ // Attach a decimal NSNumberFormatter to the "formatDecimal" key
NSNumberFormatter *decimalNumberFormatter = [[NSNumberFormatter alloc] init];
decimalNumberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
- [data setObject:decimalNumberFormatter forKey:@"DECIMAL_FORMAT"];
+ [data setObject:decimalNumberFormatter forKey:@"formatDecimal"];
/**
@@ -64,22 +64,20 @@ - (NSString *)render
* template.
*/
- // Attach a float to the "float" key
[data setObject:[NSNumber numberWithFloat:0.5] forKey:@"float"];
/**
- * Render. The formatting of numbers will happen in the
- * GRMustacheTemplateDelegate methods, hereafter.
+ * Render.
*/
NSString *templateString = @"raw: {{float}}\n"
- @"{{#PERCENT_FORMAT}}"
+ @"{{#formatPercent}}"
@"percent: {{float}}\n"
- @"{{/PERCENT_FORMAT}}"
- @"{{#DECIMAL_FORMAT}}"
+ @"{{/formatPercent}}"
+ @"{{#formatDecimal}}"
@"decimal: {{float}}\n"
- @"{{/DECIMAL_FORMAT}}";
+ @"{{/formatDecimal}}";
GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
return [template renderObject:data];
}
@@ -87,6 +85,11 @@ - (NSString *)render
@end
+/**
+ * By conforming to the GRMustacheTemplateDelegate protocol,
+ * NSNumberFormatter instances will be able to observe and alter
+ * the rendering of templates.
+ */
@interface NSNumberFormatter(GRMustache)<GRMustacheTemplateDelegate>
@end
Please sign in to comment.
Something went wrong with that request. Please try again.