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

Partials with relative paths can collide #173

Open
dak opened this issue Mar 30, 2014 · 1 comment
Open

Partials with relative paths can collide #173

dak opened this issue Mar 30, 2014 · 1 comment

Comments

@dak
Copy link

dak commented Mar 30, 2014

If two different template partials are being used with the same relative path, they collide and one will overwrite the other.

For example:
/example/abc/template.html with {{> ./partial.html}}
and
/example/def/ghi/template2.html with {{> ./partial.html}}

@brandonmcquarie
Copy link
Contributor

Unfortunately I ran into this issue as well. It doesn't look like this is an issue of the require-handlebars-plugin but rather how Handlebars.js handles its partials. Once you use the > ./partials.html it associates the shorthand of ./partial.html to the absolute path of where that file was found. In order to get around the functionality, if you use RequireJS on your project you can do something like this.

-- YOUR PROJECT FILE --

define([
    'AppFramework',
    'hbs!/example/abc/template.html',
    'hbs!/example/def/ghi/template2.hbml'
], function(AppFramework, express, http, module, _, Functions, Api, Template, PartialTemplate) {

Then farther down when you call your template....

Or you could do your res.render, for my project we needed to have res.write for additional information written later down in the project.

res.write(Template({
    title: 'Hello',
    Template2: PartialTemplate
}));

Create a helper, I named mine 'load', and in your Template.html file include the below code to load the partial template.

{{{ load 'Template2' }}}
{{{ load Template2 }}}

The 'load' helper needs to be located in your helpers directory (Typcally /templates/helpers). Here is the one I wrote for my project. The 'Template2' would fall in the first If statement, second would fall in the else if. You can enforce wether or not you want to allow strings or only functions passed by removing logic from below. I also have this idea of this._templates in there as well, what you can do in your project is declare a global variable of _template in your define module and set all of your templates there, then pass _templates to your template file, so you have access to all of the templates declared in the _templates object without having to pass every partial to every template.

_templates: {
    Template2: PartialTemplate
}


this._res.write(Template({
    _templates: this._templates
}));

-- Handlebars Helper --

define(['hbs/handlebars', 'Functions', 'Underscore'], function ( Handlebars, functions, _ ) {
    Handlebars.registerHelper('load',  function ( template, options ) {
        if (_.isString(template)) {
            if (_.isFunction(this[template])) {
                return this[template](this);
            } else if (_.isFunction(this._templates[template])) {
                return this._templates[template](this);
            } else {
                return 'Template: ' + template + ' not found!';
            }
        } else if (_.isFunction(template) {
            return template(this);
        } else {
            return 'Invalid attribute passed to Load Helper';
        }
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants