Skip to content

Load new helpers from helpers.js.#110

Merged
joanniclaborde merged 1 commit into
masterfrom
site-helpers
Aug 1, 2014
Merged

Load new helpers from helpers.js.#110
joanniclaborde merged 1 commit into
masterfrom
site-helpers

Conversation

@joanniclaborde
Copy link
Copy Markdown
Contributor

Custom Handlebars helpers can be defined in /helpers.js. This file needs to export an object defining new helpers, or existing helpers to replace.

module.exports = {
  // New helper
  frenchify: function(string) {
    return 'Le ' + string + ' sacrebleu !';
  },

  // Replaces handlebars-helper's uppercase helper
  uppercase: function(string) {
    return string + ' is uppercase';
  },
};

@jameslovejoy
Copy link
Copy Markdown

If a page has a preprocessor, this conflicts with it since they both define module.exports (the preprocessor wins and the helpers are undefined). Is there a way to make them work together?

@joanniclaborde
Copy link
Copy Markdown
Contributor Author

I'm not sure I understand. Preprocessors are defined in /preprocessors/*.js while the helpers are in /helpers.js:

/views/index.hbs

{{!
{
  "name": "index",
  "resources": {...},
  "preprocessor": "index.js"
}
}}
{{frenchify "cool"}}

/preprocessors/index.js

module.exports = function(context) {
  // ...
  return context;
};

/helpers.js

module.exports = {
  frenchify: function(string) {
    return 'Le ' + string + ' sacrebleu !';
  }
};

@joanniclaborde
Copy link
Copy Markdown
Contributor Author

Oh and helpers are shared by all views, if that's what you meant? i.e. they are not defined in preprocessors. But that might be a good idea, can you, or @pushred, think of a use case for this?

@jameslovejoy
Copy link
Copy Markdown

I think it's useful to be shared. I'm writing helpers for use in widget templates, which appear all over the site.

As for the conflict, if you update the helpers.hbs test file to be

{{!
{
  "layout": false,
  "preprocessor": "index.js"
}
}}
{{uppercase "Handlebars helpers loaded"}}
{{frenchify "Site helpers loaded"}}

Then the test fails

Error: Missing helper: 'frenchify'
    at Object.<anonymous> (/Users/james/git/solidus/node_modules/handlebars/lib/handlebars/base.js:48:11)
    at Object.eval (eval at <anonymous> (/Users/james/git/solidus/node_modules/handlebars/lib/handlebars/compiler/compiler.js:579:23), <anonymous>:12:157)
    at /Users/james/git/solidus/node_modules/handlebars/lib/handlebars/runtime.js:38:33
    at /Users/james/git/solidus/node_modules/handlebars/lib/handlebars/compiler/compiler.js:1294:21
    at ExpressHandlebars.extend._renderTemplate (/Users/james/git/solidus/node_modules/express3-handlebars/lib/express-handlebars.js:307:22)
    at ExpressHandlebars.renderTemplate (/Users/james/git/solidus/node_modules/express3-handlebars/lib/express-handlebars.js:164:18)
    at fn (/Users/james/git/solidus/node_modules/async/lib/async.js:582:34)
    at Object._onImmediate (/Users/james/git/solidus/node_modules/async/lib/async.js:498:34)
    at processImmediate [as _immediateCallback] (timers.js:336:15)
      1) Loads handlebars helpers

This is the same error I'm getting. When I remove the preprocessor from the page header, then the helper works again. I'm restarting the server, but that doesn't help.

@joanniclaborde
Copy link
Copy Markdown
Contributor Author

@jameslovejoy I found the problem, it's fixed.

@jameslovejoy
Copy link
Copy Markdown

Works now 👍

@pushred pushred added this to the 1.0.0 alpha milestone Jul 11, 2014
joanniclaborde added a commit that referenced this pull request Aug 1, 2014
Load new helpers from helpers.js.
@joanniclaborde joanniclaborde merged commit b38c85e into master Aug 1, 2014
@joanniclaborde joanniclaborde deleted the site-helpers branch August 1, 2014 15:27
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

Successfully merging this pull request may close these issues.

3 participants