davis / plugin-hbs

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

Registered Helpers not running. #7

Closed belackriv closed 8 years ago

belackriv commented 8 years ago

I can't seem to get registered helpers to run with the latest version. I'm pretty sure this has something to do the the precompile, since when I change hbs.js to this:

'use strict';

import Handlebars from 'handlebars';

const handlebarsRuntimePath = System.normalizeSync('handlebars/handlebars.runtime', __moduleName);

export function translate(load) {
  //var precompiled = Handlebars.precompile(load.source);
  //load.source = `module.exports = require('${handlebarsRuntimePath}').template(${precompiled});`;
  var templateSrc = load.source
    .replace(/'/g, '\\\'')
    .replace(/[\f]/g, '\\f')
    .replace(/[\b]/g, '\\b')
    .replace(/[\n]/g, '\\n')
    .replace(/[\t]/g, '\\t')
    .replace(/[\r]/g, '\\r')
    .replace(/[\u2028]/g, '\\u2028')
    .replace(/[\u2029]/g, '\\u2029');
  load.source = `module.exports = require('handlebars').compile('${templateSrc}');`;
}

they start working again. I don't know enough about bundling and handlebars to know how this will break things in the future though. Any advice would be appreciated.

davis commented 8 years ago

Thanks, will look into this!

Schlaefer commented 8 years ago

Same problem here, registered helpers do not work in 1.2.1. Downgraded to 1.1 as workaround.

davis commented 8 years ago

@belackriv @Schlaefer Remember that the plugin is looking at the runtime environment for the helpers, so make sure in your code you are using:

import Handlebars from 'handlebars/handlebars.runtime';
Handlebars.registerHelper(/* helper stuff */);
davis commented 8 years ago

I've updated the README with this stuff. Thanks for all the help! Let me know if you still have issues.

Schlaefer commented 8 years ago

@davis That was the issue, thanks for pointing out.