Skip to content

Instantly share code, notes, and snippets.

@groue
Created September 29, 2012 11:05
Show Gist options
  • Save groue/3803707 to your computer and use it in GitHub Desktop.
Save groue/3803707 to your computer and use it in GitHub Desktop.
GRMustache: variadic filters
{{#object1}}
{{ dateFormat(date, format) }}
{{/object1}}
{{#object2}}
{{ dateFormat(date) }}
{{/object2}}
id filters = @{
@"dateFormat": [GRMustacheFilter variadicFilterWithBlock:^id(NSArray *arguments) {
// first argument is date
NSDate *date = [arguments objectAtIndex:0];
// second (optional) argument is format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
if (arguments.count > 1)
dateFormatter.format = [arguments objectAtIndex:1];
else
dateFormatter.format = @"yyyy-MM-dd";
// compute the result
return [dateFormatter stringFromDate:date];
}]
};
id data = @{
@"object1": @{
@"format": @"yyyy-MM-dd 'at' HH:mm",
@"date": [NSDate date]
},
@"object2": @{
@"date": [NSDate date]
}
};
NSString *rendering = [GRMustacheTemplate renderObject:data
withFilters:filters
fromResource:@"base"
bundle:nil
error:NULL];
2012-10-20 at 15:05
2012-10-20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment