Skip to content

Instantly share code, notes, and snippets.

@groue
Created September 22, 2012 11:03
Show Gist options
  • Save groue/3765844 to your computer and use it in GitHub Desktop.
Save groue/3765844 to your computer and use it in GitHub Desktop.
GRMustache: variable tag that expand into a template string
{{#movie}}
{{link}}
{{#director}}
by {{link}}
{{/director}}
{{/movie}}
id data = @{
@"movie": @{
@"url": @"/movies/123",
@"title": @"Citizen Kane",
@"link": [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
NSString *templateString = @"<a href=\"{{url}}\">{{title}}</a>";
GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
return [template renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
}],
@"director": @{
@"url": @"/people/321",
@"firstName": @"Orson",
@"lastName": @"Welles",
@"link": [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
NSString *templateString = @"<a href=\"{{url}}\">{{firstName}} {{lastName}}</a>";
GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
return [template renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
}],
}
}
}
NSString *rendering = [GRMustacheTemplate renderObject:data
fromResource:@"Document"
bundle:nil
error:NULL];
<a href="/movies/123">Citizen Kane</a>
by <a href="/people/321">Orson Welles</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment