solidusjs / solidus

A simple server that generates pages from JSON and Templates
MIT License
28 stars 7 forks source link

Load new helpers from helpers.js. #110

Closed joanniclaborde closed 10 years ago

joanniclaborde commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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

jameslovejoy commented 10 years ago

Works now :+1: