Skip to content
Newer
Older
100644 183 lines (134 sloc) 5.76 KB
2e61bf9 @groue Fix internal guide links
authored
1 [up](../../../../tree/master/Guides/sample_code), [next](indexes.md)
18da0ef @groue Number formatting sample code
authored
2
3 Number formatting
4 =================
5
0dbc221 @groue 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 @groue 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 @groue 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 @groue number_formatting.md and indexes.md consistency
authored
20 ### 1st genuine Mustache technique: NSDictionary
18da0ef @groue Number formatting sample code
authored
21
22 Let's render the simple template:
23
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
24 raw: {{ value }}
25 percent: {{ percent }}
26 decimal: {{ decimal }}
18da0ef @groue Number formatting sample code
authored
27
a68a579 @groue 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 @groue Number formatting sample code
authored
29
30 ```objc
fae0cef @groue Rewritten number formatting guide, so that it uses filters.
authored
31 // The raw number
8dcc514 @groue Use Objective-C literals in guides
authored
32 NSNumber *value = @0.5:
18da0ef @groue Number formatting sample code
authored
33
fae0cef @groue Rewritten number formatting guide, so that it uses filters.
authored
34 // NSNumberFormatter objects knows how to format numbers
18da0ef @groue Number formatting sample code
authored
35 NSNumberFormatter *percentNumberFormatter = [[NSNumberFormatter alloc] init];
36 percentNumberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
a68a579 @groue 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 @groue Number formatting sample code
authored
42
f5f8e18 @groue Rewritten number formatting guide, so that it uses filters.
authored
43 // Render "raw: 0.5, percent: 50 %, decimal: 0,5"
8dcc514 @groue Use Objective-C literals in guides
authored
44 NSDictionary *dictionary = @{
45 @"value": value,
46 @"percent": percent,
47 @"decimal": decimal
48 };
18da0ef @groue Number formatting sample code
authored
49 NSString *rendering = [template renderObject:dictionary];
50 ```
51
79020b4 @groue number_formatting.md and indexes.md consistency
authored
52 ### 2nd genuine Mustache technique: specific properties
18da0ef @groue Number formatting sample code
authored
53
35162b7 @groue Hard wrap documentation to 80 columns
authored
54 Often, data comes from your model objects, not from a hand-crafted NSDictionary.
18da0ef @groue Number formatting sample code
authored
55
a68a579 @groue 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 @groue Number formatting sample code
authored
57
58 ```objc
a68a579 @groue 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 @groue Number formatting sample code
authored
64 @property (readonly) NSString *percent;
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
65 @property (readonly) NSString *decimal;
18da0ef @groue Number formatting sample code
authored
66 @end
67
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
68 @implementation Model(GRMustache)
18da0ef @groue Number formatting sample code
authored
69 - (NSString *)percent
70 {
a68a579 @groue 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 @groue Number formatting sample code
authored
81 }
82 @end
83 ```
84
85 You would then render normally:
86
87 ```objc
f5f8e18 @groue Rewritten number formatting guide, so that it uses filters.
authored
88 // Render "raw: 0.5, percent: 50 %, decimal: 0,5"
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
89 Model *model = ...
90 model.value = 0.5;
18da0ef @groue Number formatting sample code
authored
91 NSString *rendering = [template renderObject:model];
92 ```
93
9634de6 @groue Mention tag delegates as another way to format numbers
authored
94 Tag delegates
95 -------------
96
97 [Tag delegates](delegate.md) allow all numbers in a section to be formatted. For instance, in the following template, all numbers would be formatted as currencies:
98
99 {{#currency}}
100 {{#items}}
101 {{name}}: {{price}}
102 {{/items}}
103 total: {{total}}
104 taxes: {{taxes}}
105 {{/currency}}
106
107 You'll find the code in the [Tag Delegates Guide](delegate.md#altering-the-rendering-of-tags-in-a-section).
108
109 Filters
110 -------
18da0ef @groue Number formatting sample code
authored
111
c1aaf13 @groue More links from guides to sample projects
authored
112 **[Download the code](../../../../tree/master/Guides/sample_code/number_formatting)**
3f50ab0 @groue Links to sample code projects hosted at https://github.com/groue/GRMu…
authored
113
b2947e1 @groue Wording
authored
114 Let's first rewrite our initial template so that it uses filters:
18da0ef @groue Number formatting sample code
authored
115
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
116 raw: {{ value }}
117 percent: {{ percent(value) }}
118 decimal: {{ decimal(value) }}
18da0ef @groue Number formatting sample code
authored
119
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
120 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 @groue Number formatting sample code
authored
121
122 ```objc
123 - (NSString *)render
124 {
125 /**
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
126 * Our template wants to render floats in various formats: raw, or formatted
127 * as percentage, or formatted as decimal.
18da0ef @groue Number formatting sample code
authored
128 */
a5c96e1 @groue Number Formatting Sample Code update
authored
129
38d683c @groue Filters and empty closing section {{/}} are now accepted without the …
authored
130 NSString *templateString = @"raw: {{ value }}\n"
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
131 @"percent: {{ percent(value) }}\n"
132 @"decimal: {{ decimal(value) }}";
133 GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
18da0ef @groue Number formatting sample code
authored
134
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
135 /**
a5c96e1 @groue Number Formatting Sample Code update
authored
136 * Let's define the `percent` and `decimal` filters:
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
137 *
138 * Filters have to be objects that conform to the GRMustacheFilter protocol.
139 * The easiest way to build one is to use the
140 * [GRMustacheFilter filterWithBlock:] method.
141 *
142 * The formatting itself is done by our friend NSNumberFormatter.
143 */
144
18da0ef @groue Number formatting sample code
authored
145 NSNumberFormatter *percentNumberFormatter = [[NSNumberFormatter alloc] init];
146 percentNumberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
147
18da0ef @groue Number formatting sample code
authored
148 NSNumberFormatter *decimalNumberFormatter = [[NSNumberFormatter alloc] init];
149 decimalNumberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
150
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
151 id percentFilter = [GRMustacheFilter filterWithBlock:^id(id value) {
152 return [percentNumberFormatter stringFromNumber:value];
153 }];
18da0ef @groue Number formatting sample code
authored
154
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
155 id decimalFilter = [GRMustacheFilter filterWithBlock:^id(id value) {
156 return [decimalNumberFormatter stringFromNumber:value];
157 }];
18da0ef @groue Number formatting sample code
authored
158
159
160 /**
a5c96e1 @groue Number Formatting Sample Code update
authored
161 * We use a NSDictionary for storing our data, but you can use any
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
162 * other KVC-compliant container.
35162b7 @groue Hard wrap documentation to 80 columns
authored
163 */
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
164
a5c96e1 @groue Number Formatting Sample Code update
authored
165 id data = @{
8dcc514 @groue Use Objective-C literals in guides
authored
166 @"percent": percentFilter,
a5c96e1 @groue Number Formatting Sample Code update
authored
167 @"decimal": decimalFilter,
168 @"value": @(0.5),
8dcc514 @groue Use Objective-C literals in guides
authored
169 };
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
170
459f57b @groue v4.1.0
authored
171
35162b7 @groue Hard wrap documentation to 80 columns
authored
172 /**
a5c96e1 @groue Number Formatting Sample Code update
authored
173 * Render.
35162b7 @groue Hard wrap documentation to 80 columns
authored
174 */
a68a579 @groue Rewritten number formatting guide, so that it uses filters.
authored
175
a5c96e1 @groue Number Formatting Sample Code update
authored
176 return [template renderObject:data error:NULL];
18da0ef @groue Number formatting sample code
authored
177 }
178 ```
179
c1aaf13 @groue More links from guides to sample projects
authored
180 **[Download the code](../../../../tree/master/Guides/sample_code/number_formatting)**
181
9634de6 @groue Mention tag delegates as another way to format numbers
authored
182
2e61bf9 @groue Fix internal guide links
authored
183 [up](../../../../tree/master/Guides/sample_code), [next](indexes.md)
Something went wrong with that request. Please try again.