Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 193 lines (140 sloc) 6.394 kb
2e61bf9 Gwendal Roué Fix internal guide links
authored
1 [up](../../../../tree/master/Guides/sample_code), [next](indexes.md)
18da0ef Gwendal Roué Number formatting sample code
authored
2
3 Number formatting
4 =================
5
0dbc221 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
6 For the purpose of demonstration, we'll render the value 0.5 as a *raw* number, as a percentage, and as a *decimal*. For instance, on a French system, we'll get the following output:
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
7
8 raw: 0.5
9 percent: 50 %
10 decimal: 0,5
11
12
18da0ef Gwendal Roué Number formatting sample code
authored
13 In a genuine Mustache way
14 -------------------------
15
16 Mustache is a simple template language. This is why there are so many [other Mustache implementations](https://github.com/defunkt/mustache/wiki/Other-Mustache-implementations).
17
18 If your goal is to design your templates so that they are compatible with those, the best way to format numbers is to have your data objects provide those formatted numbers.
19
79020b4 Gwendal Roué number_formatting.md and indexes.md consistency
authored
20 ### 1st genuine Mustache technique: NSDictionary
18da0ef Gwendal Roué Number formatting sample code
authored
21
22 Let's render the simple template:
23
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
24 raw: {{ value }}
25 percent: {{ percent }}
26 decimal: {{ decimal }}
18da0ef Gwendal Roué Number formatting sample code
authored
27
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
28 It's quite easy to put numbers and formatted numbers in a dictionary:
18da0ef Gwendal Roué Number formatting sample code
authored
29
30 ```objc
fae0cef Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
31 // The raw number
8dcc514 Gwendal Roué Use Objective-C literals in guides
authored
32 NSNumber *value = @0.5:
18da0ef Gwendal Roué Number formatting sample code
authored
33
fae0cef Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
34 // NSNumberFormatter objects knows how to format numbers
18da0ef Gwendal Roué Number formatting sample code
authored
35 NSNumberFormatter *percentNumberFormatter = [[NSNumberFormatter alloc] init];
36 percentNumberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
37 NSString *percent = [numberFormatter stringFromNumber:value];
38
39 NSNumberFormatter *decimalNumberFormatter = [[NSNumberFormatter alloc] init];
40 decimalNumberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
41 NSString *decimal = [numberFormatter stringFromNumber:value];
18da0ef Gwendal Roué Number formatting sample code
authored
42
f5f8e18 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
43 // Render "raw: 0.5, percent: 50 %, decimal: 0,5"
8dcc514 Gwendal Roué Use Objective-C literals in guides
authored
44 NSDictionary *dictionary = @{
45 @"value": value,
46 @"percent": percent,
47 @"decimal": decimal
48 };
18da0ef Gwendal Roué Number formatting sample code
authored
49 NSString *rendering = [template renderObject:dictionary];
50 ```
51
79020b4 Gwendal Roué number_formatting.md and indexes.md consistency
authored
52 ### 2nd genuine Mustache technique: specific properties
18da0ef Gwendal Roué Number formatting sample code
authored
53
35162b7 Gwendal Roué Hard wrap documentation to 80 columns
authored
54 Often, data comes from your model objects, not from a hand-crafted NSDictionary.
18da0ef Gwendal Roué Number formatting sample code
authored
55
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
56 In this case, the best option is to declare a category on your model object, and implement specific keys that will output the formatted numbers:
18da0ef Gwendal Roué Number formatting sample code
authored
57
58 ```objc
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
59 @interface Model
60 @property float value; // the original property provided by the model
61 @end
62
63 @interface Model(GRMustache)
18da0ef Gwendal Roué Number formatting sample code
authored
64 @property (readonly) NSString *percent;
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
65 @property (readonly) NSString *decimal;
18da0ef Gwendal Roué Number formatting sample code
authored
66 @end
67
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
68 @implementation Model(GRMustache)
18da0ef Gwendal Roué Number formatting sample code
authored
69 - (NSString *)percent
70 {
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
71 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
72 numberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
73 return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:self.value]];
74 }
75
76 - (NSString *)decimal
77 {
78 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
79 numberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
80 return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:self.value]];
18da0ef Gwendal Roué Number formatting sample code
authored
81 }
82 @end
83 ```
84
85 You would then render normally:
86
87 ```objc
f5f8e18 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
88 // Render "raw: 0.5, percent: 50 %, decimal: 0,5"
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
89 Model *model = ...
90 model.value = 0.5;
18da0ef Gwendal Roué Number formatting sample code
authored
91 NSString *rendering = [template renderObject:model];
92 ```
93
79020b4 Gwendal Roué number_formatting.md and indexes.md consistency
authored
94 GRMustache solution: filters
95 ----------------------------
18da0ef Gwendal Roué Number formatting sample code
authored
96
c1aaf13 Gwendal Roué More links from guides to sample projects
authored
97 **[Download the code](../../../../tree/master/Guides/sample_code/number_formatting)**
3f50ab0 Gwendal Roué Links to sample code projects hosted at https://github.com/groue/GRMusta...
authored
98
18da0ef Gwendal Roué Number formatting sample code
authored
99 You may ask yourself, is it worth declaring dozens of stub properties just for formatting numbers?
100
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
101 [Filters](../filters.md) are quite helpful, here. However, **it may be tedious or impossible for [other Mustache implementations](https://github.com/defunkt/mustache/wiki/Other-Mustache-implementations) to produce the same rendering.**
18da0ef Gwendal Roué Number formatting sample code
authored
102
103 So check again the genuine Mustache way, above. Or keep on reading, now that you are warned.
104
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
105 Let's first rewrite our template so that it uses filters:
18da0ef Gwendal Roué Number formatting sample code
authored
106
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
107 raw: {{ value }}
108 percent: {{ percent(value) }}
109 decimal: {{ decimal(value) }}
18da0ef Gwendal Roué Number formatting sample code
authored
110
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
111 After we have told GRMustache how the `percent` and `decimal` filters should process their input, we will be releived from the need to prepare our data before it is rendered: no more adding of specific keys in a dictionary, no more declaration of a category on our models.
18da0ef Gwendal Roué Number formatting sample code
authored
112
113 ```objc
114 - (NSString *)render
115 {
116 /**
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
117 * Our template wants to render floats in various formats: raw, or formatted
118 * as percentage, or formatted as decimal.
119 *
120 * This is typically a job for filters: we'll define the `percent` and
121 * `decimal` filters.
122 *
38d683c Gwendal Roué Filters and empty closing section {{/}} are now accepted without the {{%...
authored
123 * For now, we just have our template use them.
18da0ef Gwendal Roué Number formatting sample code
authored
124 */
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
125
38d683c Gwendal Roué Filters and empty closing section {{/}} are now accepted without the {{%...
authored
126 NSString *templateString = @"raw: {{ value }}\n"
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
127 @"percent: {{ percent(value) }}\n"
128 @"decimal: {{ decimal(value) }}";
129 GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
18da0ef Gwendal Roué Number formatting sample code
authored
130
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
131 /**
132 * Now we have to define those filters.
133 *
134 * Filters have to be objects that conform to the GRMustacheFilter protocol.
135 * The easiest way to build one is to use the
136 * [GRMustacheFilter filterWithBlock:] method.
137 *
138 * The formatting itself is done by our friend NSNumberFormatter.
139 */
140
141 // Build our formatters
18da0ef Gwendal Roué Number formatting sample code
authored
142
143 NSNumberFormatter *percentNumberFormatter = [[NSNumberFormatter alloc] init];
144 percentNumberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
145
18da0ef Gwendal Roué Number formatting sample code
authored
146 NSNumberFormatter *decimalNumberFormatter = [[NSNumberFormatter alloc] init];
147 decimalNumberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
148
149
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
150 // Build our filters
18da0ef Gwendal Roué Number formatting sample code
authored
151
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
152 id percentFilter = [GRMustacheFilter filterWithBlock:^id(id value) {
153 return [percentNumberFormatter stringFromNumber:value];
154 }];
18da0ef Gwendal Roué Number formatting sample code
authored
155
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
156 id decimalFilter = [GRMustacheFilter filterWithBlock:^id(id value) {
157 return [decimalNumberFormatter stringFromNumber:value];
158 }];
18da0ef Gwendal Roué Number formatting sample code
authored
159
160
161 /**
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
162 * GRMustache does not load filters from the rendered data, but from a
163 * specific filters container.
164 *
165 * We'll use a NSDictionary for storing the filters, but you can use any
166 * other KVC-compliant container.
35162b7 Gwendal Roué Hard wrap documentation to 80 columns
authored
167 */
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
168
8dcc514 Gwendal Roué Use Objective-C literals in guides
authored
169 NSDictionary *filters = @{
170 @"percent": percentFilter,
171 @"decimal": decimalFilter
172 };
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
173
459f57b Gwendal Roué v4.1.0
authored
174
35162b7 Gwendal Roué Hard wrap documentation to 80 columns
authored
175 /**
f5f8e18 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
176 * Prepare our data
35162b7 Gwendal Roué Hard wrap documentation to 80 columns
authored
177 */
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
178
179 Model *model = ...;
180 model.value = 0.5;
f5f8e18 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
181
182
183 /**
184 * Render "raw: 0.5, percent: 50 %, decimal: 0,5"
185 */
186
a68a579 Gwendal Roué Rewritten number formatting guide, so that it uses filters.
authored
187 return [template renderObject:model withFilters:filters];
18da0ef Gwendal Roué Number formatting sample code
authored
188 }
189 ```
190
c1aaf13 Gwendal Roué More links from guides to sample projects
authored
191 **[Download the code](../../../../tree/master/Guides/sample_code/number_formatting)**
192
2e61bf9 Gwendal Roué Fix internal guide links
authored
193 [up](../../../../tree/master/Guides/sample_code), [next](indexes.md)
Something went wrong with that request. Please try again.