davis / plugin-hbs

Handlebars template loader plugin for SystemJS
MIT License
21 stars 9 forks source link

handlebars partials support is not convenient. #9

Open larryhe opened 8 years ago

larryhe commented 8 years ago

Hi @davis , this is a great SystemJs plugin. Really love it. but its handlebar partials support is not really convenient. because for each partials, you have to manually register it beforehand. So how can I make it automatically register partials while parsing handlebars template? I know require-handlebars-plugin does this type of things. (https://github.com/SlexAxton/require-handlebars-plugin/blob/master/hbs.js). Thanks for the great job.

davis commented 8 years ago

Hey Larry, could you provide an example of what you mean? Thanks!

larryhe commented 8 years ago

Hi @davis , I mean this plugin should be able to parse handlebar template and automatically register partials. for example. I have below handlebar template. the plugin should extract /path/to/myPartial out and automatically register it. requirejs-handlebar-plugin works that way. It's the same thing for helper. Not sure If i am making sense. Thanks.

{{> /path/to/myPartial }}
BennettJames commented 8 years ago

This is something I recently dealt with in a custom handlebars plugin. Here's a basic overview of how it works:

export function fetch(load, fetcher) {
  return fetcher(load)
    .then(templateSrc => {
      let partials = findPartials(Handlebars.parse(templateSrc));
      // adds partials to dependencies
      return templateSrc;
    });
}
load.metadata.deps = partials.map(p => `${partial}!hbsTmpl`);
let depRequires = load.metadata.deps.map(d => `require("${d}");`;
load.source = `
${depRequires.join('\n')}
require('${handlebarsRuntimePath}').registerPartial(${load.name}, Handlebars.template(${precompiled});
`;

It's rough, but I could fold it up into a PR if there's interest. There's a few points that are definitely sub-par that will need some work before it's really "merge ready", though.

davis commented 8 years ago

@BennettJames would definitely be open to PRs!