Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Helper: Each Include Parent - (Works with partials) #385

Closed
wants to merge 1 commit into from
Closed

New Helper: Each Include Parent - (Works with partials) #385

wants to merge 1 commit into from

Conversation

AndrewHenderson
Copy link

This helper allows a parent context to be accessed when using partial templates inside of an each.
Note: It has no dependencies other than Handlebars. Uses a local _each function that is a port of jQuey.each.
Usage: {{#eachIncludeParent context parent=this}}{{> template-partial }}{{/eachIncludeParent}}
See demo: jsFiddle - http://jsfiddle.net/AndrewHenderson/kQZpu/5/
Comment Thread: #182 (comment)

This helper allows a parent context to be accessed when using partial templates.
Note: It has no dependencies other than Handlebars. Uses a local _each function that is a port of jQuey.each.
Usage: {{#eachIncludeParent context parent=this}}{{> template-partial }}{{/eachIncludeParent}}
See demo: jsFiddle - http://jsfiddle.net/AndrewHenderson/kQZpu/4/
Comment Thread: #182 (comment)
@mpetrovich
Copy link
Contributor

I think this might be a better approach: #368

@AndrewHenderson
Copy link
Author

I like this approach, however it fails the build test. I tried to get to work on JsFiddle (http://jsfiddle.net/kQZpu/11/), but I get an error. There seems to be an issue with the return statement:

return Handlebars.SafeString(Handlebars.VM.invokePartial(Handlebars.partials[name], name, context, {}, Handlebars.partials));

That line throws the following error, which you can see in console:

Uncaught TypeError: Cannot call method 'call' of undefined

I tried investigating those Handlebars methods, but they're not documented and are difficult to trace.

@mpetrovich
Copy link
Contributor

One issue may be that you're using an older version of Handlebars (1.0.0beta6). Use this version, which is one of the last releases (post-1.0.0.rc1): https://dl.dropbox.com/u/22528488/handlebars.js

@AndrewHenderson
Copy link
Author

Thanks. I updated the version. No longer receive the error, however the context is lost an variables are returned undefined. http://jsfiddle.net/AndrewHenderson/kQZpu/13/. Look in console for logging.

Have you been able to get this to work in a reduced test case?

@pward123
Copy link

pward123 commented Aug 2, 2013

It looks like you must merge the template name into the hash. If you keep them separate, it goofs the options hash
http://jsfiddle.net/QV9em/3/

@pward123
Copy link

pward123 commented Aug 2, 2013

FWIW, I prefer to just have the partial called like so...

<script id="template" type="text/x-handlebars-template">
    <!-- with partial -->
    {{#each hobbies}}
        {{#include parent=.. }}
            {{> template-partial}}
        {{/include}}
    {{/each}}
</script>

See http://jsfiddle.net/QV9em/4/ for a working sample

@kpdecker
Copy link
Collaborator

I'm sorry for not giving this any attention after being opened so long ago. The hash passing approach is a good one but this should be part of the core partials syntax as it avoids the caveat above and make this a proper first level implementation. I will provide an update on #182 implementing something to that effect.

@kpdecker kpdecker closed this Jan 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants