I read this post http://tilomitra.com/sailsjs-with-handlebars/ ,but I don't know how to add Helpers support.
1) Add a file in /config folder. (example: /config/helper.js)
2) add these codes:
Handlebars = require('handlebars');
Handlebars.registerHelper('helper name',yourHelper);
3) The helpers you register here can be used in your .handlebar files.
Hope this help :)
BTW:
stackoverflow might be a better place to ask question.
@giyyapan Helper is fine, but partials dosen't work ?
Like below
views/
layouts/
main.handlebars
partials/
partial1.handlebars
partial2.handlebars
I use this https://github.com/jmtexier/sails-generate-views-handlebars fix my problem!
layout ,partials ,helper are support
npm install sails-generate-views-handlebars
sails generate views-handlebars
modify config/views.js
layout: 'layouts/layout',
partials: 'partials',
helpers: require('./helpers')
add new file helpers.js
var Handlebars = require('sails/node_modules/express-handlebars/node_modules/handlebars');
Handlebars.registerHelper('help', function() {
return "help22";
});
I'm using the technique described above, but am receiving the following error:
{
message: "Missing helper: "eq"",
name: "Error"
}
I'm not sure if the double quote after "eq" is indicative of an error in the package, or my code.
Desired use:
{{#eq val1 "val2"}}text{{/eq}
How to do this in v0.11.0 . I think I cant directly define helper in a config file in this version . They need to go inside a function which can be exported using module.exports.
@stewhouston - Were you able to get this working ?
@pawanrawal - Pardon the late reply, my project was shelved for quite some time. I did not get registerHelper working as referenced above, but did get an inline/ad hoc res.view locals.helpers method to work. However it's undesirable to attach a reusable template method inside a controller.
I've been away from Sails for a bit, so if you did get this working it would still be of interest.
Just grabbing this up. Is there any solution? I have defined a math helper in config/helpers.js and try to use it in a partial template.
helpers.js:
``
Handlebars = require( 'handlebars' );
Handlebars.registerHelper( "math", function ( lvalue, operator, rvalue, options )
{
lvalue = parseFloat( lvalue );
rvalue = parseFloat( rvalue );
return {
"+": lvalue + rvalue,
"-": lvalue - rvalue,
"*": lvalue * rvalue,
"/": lvalue / rvalue,
"%": lvalue % rvalue
}[ operator ];
} );
``
Console output:
error: Sending 500 ("Server Error") response:
Error: Missing helper: "math"
at Object.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
at Object.eval (eval at createFunctionContext (/usr/local/lib/node_modules/sails/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:18:66)
at main (/usr/local/lib/node_modules/sails/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
at ret (/usr/local/lib/node_modules/sails/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
at ret (/usr/local/lib/node_modules/sails/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
at ExpressHandlebars._renderTemplate (/usr/local/lib/node_modules/sails/node_modules/express-handlebars/lib/express-handlebars.js:247:12)
at ExpressHandlebars.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/express-handlebars/lib/express-handlebars.js:173:21) { [Error: Missing helper: "math"]
description: undefined,
fileName: undefined,
lineNumber: undefined,
message: 'Missing helper: "math"',
name: 'Error',
number: undefined }
The problem depends which node (actually which npm version you use). Starting with node >= 5, npmv3 gets shipped with it, which calculates a flat node_modules hierarchy out of you dependency tree and only installs nested node_modules if necessary.
Anyway... If you are using node >= 5 it should work when you use require('handlebars') but if you use node < 5 (e.g. v4.x) you have to require the handlebars module, sails uses for rendering your views, otherwise you would register the helper correctly, but on the wrong Handlebar module and the helper can not be found.
So:
node < 5 (e.g. v4.x):
var Handlebars = require('sails/node_modules/express-handlebars/node_modules/handlebars')
Handlebars.registerHelper('foo', function() { return 'foo' })
node >= 5:
var Handlebars = require('handlebars')
Handlebars.registerHelper('foo', function() { return 'foo' })
I put that directly on top of the config/views.js, but it should work in any config file, also in config/helpers.js
I'm using node v5.0.0.
var Handlebars = require('handlebars'); returns the correct object and I can add my math helper function. That's not the problem...
The problem is, that I can't use my defined math helper in a partial template like in the example of @shanelau
I'm having issues with helpers: require('./helpers') in my config/views.js. When I run sails lift I get Cannot find module './helpers'. Any thoughts?
@liamanderson You need create a new file helpers.js in your project

FYI, you should always install handlebars directly into your project rather than trying to require the copy from inside Sails.
@Crease29 if you're having a separate problem, please create a new issue and we'll check it out!
Hi i was facing the same issue here. so i took a look to the source file layoutshim.js
It is using sails.config.views.helpers to initialize the helpers object on express-handlebars.
// Use express-handlebars for handlebars templates, and set up layout functionality
else if (engineName === 'handlebars') {
var exphbs = require('express-handlebars');
sails.log.verbose('Overriding handlebars engine with express-handlebars to implement layout support...');
var hbs = exphbs.create({
defaultLayout: path.join('..', (sails.config.views.layout + '.' + (extension || 'handlebars')) || ''),
helpers: sails.config.views.helpers || {},
partialsDir: path.join('views', sails.config.views.partials || ''),
extname: extension || 'handlebars'
});
sails.config.views.engine.fn = hbs.engine;
So you dont need to require handlebars or registerHelper just make an object with your helper methods.
//config\helpers.js
module.exports = {
foo: function () {
return 'FOO!';
},
bar: function () {
return 'BAR!';
}
}
and set your sails.config.views.helpers to that object using require:
//config\views.js
engine: 'handlebars',
extension: 'hbs', // if you want to use .hbs extensions
helpers: require("./helpers"),
Most helpful comment
The problem depends which node (actually which npm version you use). Starting with node >= 5, npmv3 gets shipped with it, which calculates a flat node_modules hierarchy out of you dependency tree and only installs nested node_modules if necessary.
Anyway... If you are using node >= 5 it should work when you use
require('handlebars')but if you use node < 5 (e.g. v4.x) you have to require the handlebars module, sails uses for rendering your views, otherwise you would register the helper correctly, but on the wrong Handlebar module and the helper can not be found.So:
node < 5 (e.g. v4.x):
node >= 5:
I put that directly on top of the
config/views.js, but it should work in any config file, also inconfig/helpers.js