groue / GRMustache

Flexible and production-ready Mustache templates for MacOS Cocoa and iOS
http://mustache.github.com/
MIT License
1.44k stars 190 forks source link

leaving a space if number is null #61

Closed angelochen960 closed 11 years ago

angelochen960 commented 11 years ago

Hi, say I have this template: {{reference}} {{amt(debit)}} {{amt(credit)}} {{amt(balance)}}

in every line, either debit or credit will be null, so it does not render, I'd like to render a space of 10 if it is null so that the report looks aligned in proper columns, possible?

Angelo

groue commented 11 years ago

Hi @angelochen960 !

Sure it is possible.

You have basically two options:

1.Keep your template as is, and write your custom filter (look at https://github.com/groue/GRMustache/blob/master/Guides/filters.md)

  1. Change your template so that it contains boolean sections (look at https://github.com/groue/GRMustache/blob/master/Guides/runtime.md#section-tags)

Tell me if you are experiencing any difficulty.

angelochen960 commented 11 years ago

thanks for the quick response, I have something very similar to this:

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

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

NSString *rendering = [GRMustacheTemplate renderObject:data
                                          fromResource:@"Document"
                                                bundle:nil
                                                 error:NULL];

can it to be modify to have '##' if the x is 0.0? Thanks

angelochen960 commented 11 years ago

following works:

@"amt": [GRMustacheFilter filterWithBlock:^id(id object) {
                     NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
                     numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
                     numberFormatter.minimumFractionDigits =2;
                     NSLog(@"obj is %@", object);
                     if (object != [NSNull null])
                         return [numberFormatter stringFromNumber:object];
                     else
                         return @"###";
                 }]