public
Last active

GRMustache: variadic filters

  • Download Gist
base.mustache
HTML+Django
1 2 3 4 5 6
{{#object1}}
{{ dateFormat(date, format) }}
{{/object1}}
{{#object2}}
{{ dateFormat(date) }}
{{/object2}}
render.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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];
rendering.txt
1 2
2012-10-20 at 15:05
2012-10-20

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.