Skip to content

Commit

Permalink
Obsolete number formatting and localization sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Feb 13, 2013
1 parent 422c55a commit bd53b5f
Show file tree
Hide file tree
Showing 26 changed files with 22 additions and 6,525 deletions.
4 changes: 2 additions & 2 deletions Guides/compatibility.md
Expand Up @@ -144,9 +144,9 @@ Tag delegates

GRMustache's [tag delegates](delegate.md), unknown to the Mustache specification, let you observe, and possibly alter the rendering of the Mustache tags.

Tag delegates may be used for formatting values in a spec-compliant way (see sample code in [Tag Delegates Guide](delegate.md)). They may also be used for putting Mustache on steroids, as in the [Localization Sample Code](sample_code/localization.md).
Tag delegates may be used for formatting values in a spec-compliant way (see sample code in [Tag Delegates Guide](delegate.md)). They may also at the core of many items of the [standard library](standard_library.md).

As such, they are an ambiguous tool. You will have to know when you cross the line.
They are an ambiguous tool. You will have to know when you cross the line.


[up](../../../../GRMustache#documentation), [next](configuration.md)
71 changes: 8 additions & 63 deletions Guides/delegate.md
Expand Up @@ -150,67 +150,17 @@ You can, for instance, provide default rendering for missing values:
[[Document new] render];
```

### Altering the rendering of tags in a section

As stated above, when a section renders an object that conforms to the `GRMustacheTagDelegate` protocol, this object observes the rendering of all tags inside the section.

The [Localization Sample Code](sample_code/localization.md) will give us an example, but let's have fun with numbers, and have Mustache format all numbers in a section attached to a `NSNumberFormatter` instance:

```objc
// Have NSNumberFormatter conform to the GRMustacheTagDelegate protocol,
// so that a formatter can format all numbers in a section:
@interface NSNumberFormatter(Document)<GRMustacheTagDelegate>
@end

@implementation NSNumberFormatter(Document)

- (id)mustacheTag:(GRMustacheTag *)tag willRenderObject:(id)object
{
// Format all numbers that happen to be rendered by variable tags such as
// `{{ count }}`.
//
// We avoid messing with sections, since they rely on boolean values of
// numbers.

if (tag.type == GRMustacheTagTypeVariable && [object isKindOfClass:[NSNumber class]]) {
return [self stringFromNumber:object];
}
return object;
}

@end

NSString *templateString = @"x = {{x}}\n"
@"{{#percent}}x = {{x}}{{/percent}}\n"
@"{{#decimal}}x = {{x}}{{/decimal}}";
GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];

NSNumberFormatter *percentFormatter = [NSNumberFormatter new];
percentFormatter.numberStyle = NSNumberFormatterPercentStyle;

NSNumberFormatter *decimalFormatter = [NSNumberFormatter new];
decimalFormatter.numberStyle = NSNumberFormatterDecimalStyle;

id data = @{
@"x": @(0.5),
@"percent": percentFormatter,
@"decimal": decimalFormatter
};

// On a French system:
// x = 0.5
// x = 50 %
// x = 0,5
NSString *rendering = [template renderObject:data error:NULL];
```

Tag Delegates as Cross-Platform Filters
---------------------------------------

Let's consider again the number formatting example above. We were able to render `{{#percent}}x = {{x}}{{/percent}}` as `x = 50 %`: The tag delegate attached to the `percent` has formatted the number `x` as a percentage.
Tag delegates can observe, but also *alter* the rendering of all tags inside a section or the full template.

Let's consider the behavior of NSFormatter in GRMustache. They are able to format all variable tags inside a section (check the [NSFormatter Guide](NSFormatter.md)).

You could also use [filters](filters.md) in order to format numbers: `x = {{ percent(x) }}` would render just as well.
For example, `{{#percent}}x = {{x}}{{/percent}}` renders as `x = 50 %` when `percent` is attached to an NSNumberFormatter. That is because formatters are *tag delegates*.

We could also use [filters](filters.md) in order to format numbers: `x = {{ percent(x) }}` would render just as well.

However, `{{#percent}}x = {{x}}{{/percent}}` has one advantage over `x = {{ percent(x) }}`: it uses plain Mustache syntax, and is compatible with [other Mustache implementations](https://github.com/defunkt/mustache/wiki/Other-Mustache-implementations).

Expand All @@ -222,7 +172,7 @@ With such a common template, it's now a matter of providing different data, depe
// data for GRMustache
{
"x": 0.5,
"percent": the_formating_tag_delegate
"percent": (some well-configured NSNumberFormatter)
}

// data for other Mustache implementations
Expand Down Expand Up @@ -276,6 +226,7 @@ NSString *rendering = [GRMustacheTemplate renderObject:data
The final rendering is "JOHANNES KEPLER".
Compatibility with other Mustache implementations
-------------------------------------------------
Expand All @@ -284,10 +235,4 @@ The [Mustache specification](https://github.com/mustache/spec) does not have the
**As a consequence, if your goal is to design templates that remain compatible with [other Mustache implementations](https://github.com/defunkt/mustache/wiki/Other-Mustache-implementations), use `GRMustacheTagDelegate` with great care.**
Sample code
-----------

The [Localization Sample Code](sample_code/localization.md) uses tag delegates for localizing portions of a template.


[up](../../../../GRMustache#documentation), [next](rendering_objects.md)
2 changes: 1 addition & 1 deletion Guides/filters.md
Expand Up @@ -179,7 +179,7 @@ Instead, have a look at tag delegates, especially the [Tag Delegates as Cross-Pl
Sample code
-----------
Custom filters are used by the [Formatted Numbers](sample_code/number_formatting.md) and [Collection Indexes](sample_code/indexes.md) sample codes. Go check inspiration there.
Custom filters are used in the [Collection Indexes](sample_code/indexes.md) sample code. Go check inspiration there.
[up](../../../../GRMustache#documentation), [next](delegate.md)
4 changes: 1 addition & 3 deletions Guides/rendering_objects.md
Expand Up @@ -419,7 +419,7 @@ Many useful things.
When GRMustache renders `{{ name }}`, it looks for the `name` key in the [context stack](runtime.md): for the title and names of our movies and people to render, movies and people must enter the context stack. This is the reason for the derivation of new contexts, using the `contextByAddingObject:` method, before partials are rendered.
There is also a `contextByAddingTagDelegate:` method, that is documented in the [Delegates Guide](delegate.md). An interesting usage of this method is demonstrated in the [Localization Sample Code](sample_code/localization.md).
There is also a `contextByAddingTagDelegate:` method, that is illustrated in the [Delegates Guide](delegate.md).
Example: Render collections of objects
Expand Down Expand Up @@ -483,8 +483,6 @@ You *can* write specification-compliant "Mustache lambdas" with rendering object
Sample code
-----------

The [Localization Sample Code](sample_code/localization.md) uses the `GRMustacheRendering` protocol for localizing portions of template.

The [Collection Indexes Sample Code](sample_code/indexes.md) uses the `GRMustacheRendering` protocol for rendering indexes of an array items.

[up](../../../../GRMustache#documentation), [next](protected_contexts.md)
3 changes: 1 addition & 2 deletions Guides/runtime.md
Expand Up @@ -239,7 +239,6 @@ Such sections are fully documented in the [Rendering Objects Guide](rendering_ob

```objc
id data = @{
@"localize": [LocalizingHelper new],
@"name1": @"Gustave",
@"name2": @"Henriett" };

Expand All @@ -252,7 +251,7 @@ NSString *rendering = [GRMustacheTemplate renderObject:data
error:NULL];
```
This fancy `LocalizingHelper` class is described in the [Localization Sample Code](sample_code/localization.md).
The `localize` key is attached to a rendering object that is built in the [standard library](standard_library.md) shipped with GRMustache.
### Other sections
Expand Down
16 changes: 3 additions & 13 deletions Guides/sample_code/README.md
@@ -1,18 +1,8 @@
[up](../../../../../GRMustache#documentation), [next](number_formatting.md)
[up](../../../../../GRMustache#documentation), [next](indexes.md)

Sample code
===========

- [Number Formatting](number_formatting.md)
**[Collection Indexes](indexes.md)**: How to have GRMustache render array indexes, render sections for the first or the last element, for odd or even elements, etc.: *discussion, downloadable Xcode project*.

How to format numbers: *discussion, downloadable Xcode project*.

- [Collection Indexes](indexes.md)

How to have GRMustache render array indexes, render sections for the first or the last element, for odd or even elements, etc.: *discussion, downloadable Xcode project*.

- [Localization](localization.md)

How to have GRMustache localize portions of your templates: *discussion, downloadable Xcode project*.

[up](../../../../../GRMustache#documentation), [next](number_formatting.md)
[up](../../../../../GRMustache#documentation), [next](indexes.md)
4 changes: 2 additions & 2 deletions Guides/sample_code/indexes.md
@@ -1,4 +1,4 @@
[up](../../../../tree/master/Guides/sample_code), [next](localization.md)
[up](../../../../tree/master/Guides/sample_code), [next](../forking.md)

Collection Indexes
==================
Expand Down Expand Up @@ -153,4 +153,4 @@ Writing [filters](../filters.md) that return [rendering objects](../rendering_ob
**[Download the code](../../../../tree/master/Guides/sample_code/indexes)**
[up](../../../../tree/master/Guides/sample_code), [next](localization.md)
[up](../../../../tree/master/Guides/sample_code), [next](../forking.md)

0 comments on commit bd53b5f

Please sign in to comment.