Skip to content

Instantly share code, notes, and snippets.

@dbashford
Last active August 29, 2015 14:10
Show Gist options
  • Save dbashford/cc2252715dfdc581789c to your computer and use it in GitHub Desktop.
Save dbashford/cc2252715dfdc581789c to your computer and use it in GitHub Desktop.
Getting custom helpers into HTMLBars

The existing lookupHelper really only supports 3 helpers.

function lookupHelper(helperName) {
  if (helperName === 'attribute') {
    return this.attribute;
  }
  else if (helperName === 'partial'){
    return this.partial;
  }
  else if (helperName === 'concat') {
    return this.concat;
  }
}

So would this need to be extended to do something more like this? I notice in the Ember helpers the helpers on env

function lookupHelper(helperName, env) {
  if (helperName === 'attribute') {
    return this.attribute;
  }
  else if (helperName === 'partial'){
    return this.partial;
  }
  else if (helperName === 'concat') {
    return this.concat;
  }
  else if (env.helpers[helperName]) {
    return env.helpers[helperName];
  }
}

or possibly this which would certainly make it easier to drop-in replace Handlebars?

var helpers = {
  attribute: this.attribute,
  partial: this.partial,
  concat: this.concat
};
function registerHelper = function(name, fn) {
  helpers[name] = fn;
}
function lookupHelper(helperName) {
  return helpers[name];
}

or a mash of both? env and this.helpers?

function lookupHelper(helperName, env) {
  return env.helpers[helperName] || helpers[name];
}

Or, quite possibly, I've enormously off-base from the outset and I'm missing how I'd get my own helpers in place and its already there.

@mixonic
Copy link

mixonic commented Nov 20, 2014

@dbashford you have it pretty much correct. I suggest putting your helpers on env.helpers and having lookupHelper find them there. When you run a template, you will need to pass an env object with helpers and the built-in hooks.

The only difference is that hooks are called directly by the templates, and may have unique call signatures. Besides that the intended behavior is pretty much the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment