Documentation of rendering objects: https://github.com/groue/GRMustache/blob/master/Guides/rendering_objects.md
Created
September 22, 2012 11:07
-
-
Save groue/3765850 to your computer and use it in GitHub Desktop.
GRMustache: objects that render themselves
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{movie}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{title}} by {{director}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{firstName}} {{lastName}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GRMustache.h" | |
#import "Movie.h" | |
#import "Person.h" | |
// Declare categories on our classes so that they conform to the | |
// GRMustacheRendering protocol: | |
@interface Movie(GRMustache)<GRMustacheRendering> | |
@end | |
@interface Person(GRMustache)<GRMustacheRendering> | |
@end | |
// Now implement the protocol: | |
@implementation Movie(GRMustache) | |
- (NSString *)renderForMustacheTag:(GRMustacheTag *)tag context:(GRMustacheContext *)context HTMLSafe:(BOOL *)HTMLSafe error:(NSError **)error | |
{ | |
// Extract the "Movie.mustache" partial from the original templateRepository: | |
GRMustacheTemplate *partial = [tag.templateRepository templateNamed:@"Movie" error:NULL]; | |
// Add self to the top of the context stack, so that the partial | |
// can access our keys: | |
context = [context contextByAddingObject:self]; | |
// Return the rendering of the partial | |
return [partial renderContentWithContext:context HTMLSafe:HTMLSafe error:error]; | |
} | |
@end | |
@implementation Person(GRMustache) | |
- (NSString *)renderForMustacheTag:(GRMustacheTag *)tag context:(GRMustacheContext *)context HTMLSafe:(BOOL *)HTMLSafe error:(NSError **)error | |
{ | |
// Extract the "Person.mustache" partial from the original templateRepository: | |
GRMustacheTemplate *partial = [tag.templateRepository templateNamed:@"Person" error:NULL]; | |
// Add self to the top of the context stack, so that the partial | |
// can access our keys: | |
context = [context contextByAddingObject:self]; | |
// Return the rendering of the partial | |
return [partial renderContentWithContext:context HTMLSafe:HTMLSafe error:error]; | |
} | |
@end | |
// Render | |
main() { | |
@autoreleasepool { | |
Person *orsonWelles = [Person personWithFirstName:@"Orson" lastName:@"Welles"]; | |
Movie *movie = [Movie movieWithTitle:@"Citizen Kane" director:orsonWelles]; | |
id data = @{ @"movie": movie }; | |
NSString *rendering = [GRMustacheTemplate renderObject:data | |
fromResource:@"Document" | |
bundle:nil | |
error:NULL]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citizen Kane by Orson Welles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment