Skip to content

Commit

Permalink
Document important caveat of GRMustacheDateFormatterHelper and GRMust…
Browse files Browse the repository at this point in the history
…acheNumberFormatterHelper
  • Loading branch information
groue committed Mar 3, 2012
1 parent daef754 commit 63cf976
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Tests/v1.9/GRMustacheDateFormatterHelperTest.m
Expand Up @@ -44,4 +44,23 @@ - (void)testDateFormatting
STAssertEqualObjects(result, @"Thursday, January 1, 1970 12:00 AM", nil);
}

- (void)testDateFormattingFailsInSubSection
{
NSString *templateString = @"{{#format}}{{#section}}{{value}}{{/section}}{{/format}}";
GRMustacheTemplate *template = [GRMustacheTemplate parseString:templateString error:NULL];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = NSDateFormatterFullStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.doesRelativeDateFormatting = NO;
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
dateFormatter.calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDictionary *helper = [NSDictionary dictionaryWithObject:[GRMustacheDateFormatterHelper helperWithDateFormatter:dateFormatter] forKey:@"format"];

NSDictionary *data = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSDate dateWithTimeIntervalSince1970:0] forKey:@"value"] forKey:@"section"];
NSString *result = [template renderObjects:helper, data, nil];
STAssertEqualObjects(result, @"1970-01-01 00:00:00 +0000", nil);
}

@end
14 changes: 14 additions & 0 deletions Tests/v1.9/GRMustacheNumberFormatterHelperTest.m
Expand Up @@ -39,6 +39,20 @@ - (void)testNumberFormatting
STAssertEqualObjects(result, @"5E-1", nil);
}

- (void)testNumberFormattingFailsInSubSection
{
NSString *templateString = @"{{#format}}{{#section}}{{value}}{{/section}}{{/format}}";
GRMustacheTemplate *template = [GRMustacheTemplate parseString:templateString error:NULL];

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
numberFormatter.numberStyle = kCFNumberFormatterScientificStyle;
NSDictionary *helper = [NSDictionary dictionaryWithObject:[GRMustacheNumberFormatterHelper helperWithNumberFormatter:numberFormatter] forKey:@"format"];

NSDictionary *data = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.5] forKey:@"value"] forKey:@"section"];
NSString *result = [template renderObjects:helper, data, nil];
STAssertEqualObjects(result, @"0.5", nil);
}

- (void)testBooleansAreNotAffected
{
NSString *templateString = @"{{#format}}{{value}}{{/format}}";
Expand Down
2 changes: 1 addition & 1 deletion guides/date_formatting.md
Expand Up @@ -3,7 +3,7 @@
Date formatting with GRMustacheDateFormatterHelper
==================================================

**This helper class allows you to format *all* dates in a section of your template.**
**This helper class allows you to format the dates in a section of your template.**

It does not belong the the core GRMustache code, and as such must be imported separately:

Expand Down
13 changes: 11 additions & 2 deletions guides/number_formatting.md
Expand Up @@ -3,7 +3,7 @@
Number formatting with GRMustacheNumberFormatterHelper
======================================================

**This helper class allows you to format *all* numbers in a section of your template.**
**This helper class allows you to format the numbers in a section of your template.**

It does not belong the the core GRMustache code, and as such must be imported separately:

Expand Down Expand Up @@ -54,6 +54,15 @@ We just have to create two `GRMustacheNumberFormatterHelper` objects, provide th
// decimal: 0,5
[template renderObject:data];

It is worth noting that the `GRMustacheNumberFormatterHelper` is implemented on top of public GRMustache APIs. Check the [code](../Classes/GRMustacheNumberFormatterHelper.m) for inspiration, and [guides/runtime/helpers.md](runtime/helpers.md) for more information on GRMustache's take on Mustache lambda sections.
Scope
-----

GRMustacheNumberFormatterHelper does not format numbers in sub sections. For instance, if you plan to format numbers in a collection, make sure your formatter helper is invoked *inside* the collection section:

{{#items}}
{{#decimal_format}}
{{float}}
{{/decimal_format}}
{{/items}}

[up](../../../../GRMustache), [next](date_formatting.md)

0 comments on commit 63cf976

Please sign in to comment.