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

Empty template returned #69

Closed besi closed 10 years ago

besi commented 10 years ago

I have a text file which is my template for a message, that I want to compose.

Now I could get the template to work, but not with the builtin renderObject: fromResource: bundle: error: method.

So, this works

NSString *template = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FeedbackMailTemplate" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];
NSString *body = [GRMustacheTemplate renderObject:data fromString:template error:NULL];

but this doesn't work

NSString *body1 = [GRMustacheTemplate renderObject:data fromResource:@"FeedbackMailTemplate.txt" bundle:nil error:NULL];
NSString *body2 = [GRMustacheTemplate renderObject:data fromResource:@"FeedbackMailTemplate.txt" bundle:[NSBundle mainBundle] error:NULL];
groue commented 10 years ago

Hi @besi,

This is because the renderObject:fromResource:bundle:error: method expects a resource with .mustache extension (if not stated clearly enough in the documentation, I'll have to fix this).

So either rename your file to .mustache or .txt.mustache (some people do that), either use your first technique, and everything will be OK.

Should you eventually want to split your templates into sub-templates (called partials), while keeping the .txt extension, you may be interested in the GRMustacheTemplateRepository class. It can handle a set of partials with any custom file extension. Please refer to the Template Repository Guide, linked from the README file.

Happy Mustache!

besi commented 10 years ago

Wow thanks for the clarification. I will mustachen up my template then!

groue commented 10 years ago

Great! Thanks for the nice answer :-)