Permalink
Browse files

Number Formatting Sample Code update

  • Loading branch information...
1 parent a381630 commit a5c96e13f9fdc56ff3864339f1574b784be2eb77 @groue committed Nov 2, 2012
Showing with 8 additions and 28 deletions.
  1. +8 −28 Guides/sample_code/number_formatting.md
@@ -116,20 +116,15 @@ After we have told GRMustache how the `percent` and `decimal` filters should pro
/**
* Our template wants to render floats in various formats: raw, or formatted
* as percentage, or formatted as decimal.
- *
- * This is typically a job for filters: we'll define the `percent` and
- * `decimal` filters.
- *
- * For now, we just have our template use them.
*/
-
+
NSString *templateString = @"raw: {{ value }}\n"
@"percent: {{ percent(value) }}\n"
@"decimal: {{ decimal(value) }}";
GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
/**
- * Now we have to define those filters.
+ * Let's define the `percent` and `decimal` filters:
*
* Filters have to be objects that conform to the GRMustacheFilter protocol.
* The easiest way to build one is to use the
@@ -138,17 +133,12 @@ After we have told GRMustache how the `percent` and `decimal` filters should pro
* The formatting itself is done by our friend NSNumberFormatter.
*/
- // Build our formatters
-
NSNumberFormatter *percentNumberFormatter = [[NSNumberFormatter alloc] init];
percentNumberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
NSNumberFormatter *decimalNumberFormatter = [[NSNumberFormatter alloc] init];
decimalNumberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
-
- // Build our filters
-
id percentFilter = [GRMustacheFilter filterWithBlock:^id(id value) {
return [percentNumberFormatter stringFromNumber:value];
}];
@@ -159,32 +149,22 @@ After we have told GRMustache how the `percent` and `decimal` filters should pro
/**
- * GRMustache does not load filters from the rendered data, but from a
- * specific filters container.
- *
- * We'll use a NSDictionary for storing the filters, but you can use any
+ * We use a NSDictionary for storing our data, but you can use any
* other KVC-compliant container.
*/
- NSDictionary *filters = @{
+ id data = @{
@"percent": percentFilter,
- @"decimal": decimalFilter
+ @"decimal": decimalFilter,
+ @"value": @(0.5),
};
/**
- * Prepare our data
+ * Render.
*/
- Model *model = ...;
- model.value = 0.5;
-
-
- /**
- * Render "raw: 0.5, percent: 50 %, decimal: 0,5"
- */
-
- return [template renderObject:model withFilters:filters];
+ return [template renderObject:data error:NULL];
}
```

0 comments on commit a5c96e1

Please sign in to comment.