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

Search for templates in a relative path #78

Closed apauly closed 10 years ago

apauly commented 10 years ago

This commit allows rendering partials that are located in the same directory as the original template.

- templates
  - foo
    a.mustache
    b.mustache

a.mustache:

{{>b}}
groue commented 10 years ago

Hi again!

Thanks a lot. I believe I'm going to merge your commit in.

However I still do not exactly understand it.

I see that you extend the bundle-based template repository so that it can load templates stored in a resource directory. So far, so good, it's a nice idea.

But I don't know how I could clearly update the documentation to tell about this new feature. What is the problem you are trying to solve?

apauly commented 10 years ago

Consider the folder structure from my previous post.

Given the following code, I get a GRMustache error: No such template: 'b'.

NSBundle *templateBundle = [NSBundle bundleWithPath:@"..../templates"];
NSString *template = @"foo/a";

NSString *rendered = [GRMustacheTemplate renderObject:@{}
                                         fromResource:template
                                               bundle:templateBundle
                                                error:NULL];

To get it working, I have to update the template a to the following:

{{>foo/b}}

With the changes from this pull request, I don't have to change the content of the template since a.mustache is already in the foo directory. I'm sharing the templates with a ruby implementation of mustache which behaves like this. So updating the partials is not an option for me.

groue commented 10 years ago

All right, I get it. Actually I did not expect @"foo/a" to load - and did not test for it.

Thanks for your contribution. I merge your commit, and will prepare a new release soon. It may take a while, because I'm on vacation.

Is it OK for you to keep on working with your fork until the new version is out?

apauly commented 10 years ago

Take it easy, I'll use my fork for now.

groue commented 10 years ago

v7.1.0 has shipped with your contribution. Thanks, @apauly!

There is a slight modification to your initial code: {{> /partial }} (with a leading slash) always loads the partial from the root of the bundle.