handlebar helpers not available at application level
See original GitHub issueI would like to have my handlebar helpers available to all my views, but for some reason this doesn’t work:
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var logger = require('morgan');
var handlebars = require("express-handlebars");
var path = require('path');
var favicon = require('serve-favicon');
module.exports = function(app, envConfig){
// view engine setup
app.set('views', path.join(envConfig.rootPath, 'views'));
var hbs = handlebars.create({
helpers: {
foo: function () { return 'FOO!'; },
bar: function () { return 'BAR!'; }
}
});
app.engine('.hbs', handlebars({
extname: '.hbs',
defaultLayout: 'main',
layoutsDir: path.join(envConfig.rootPath,'views/layouts'),
partialsDir: path.join(envConfig.rootPath, 'views/partials')
}));
app.set('view engine', '.hbs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
secret: 'temporary',
resave: true,
saveUninitialized: true
}));
app.use(flash());
app.use(express.static(path.join(envConfig.rootPath, 'public')));
};
When I try to output that in my view I get nothing.
But if I set the function inside my render() method, it works:
var request = require('request');
var faker = require('faker');
var _ = require('lodash');
module.exports = {
index: function(req, res){
var numRecords = 10;
var profiles = [];
for(i=0; i < numRecords; i++) {
profiles.push({
full_name: faker.name.findName(),
blurb: faker.lorem.words(5),
avatar: faker.image.avatar()
});
}
res.render('profiles/index', {
profiles: profiles,
layout : 'main',
helpers: {
foo: function () { return 'FOO!'; },
bar: function () { return 'BAR!'; }
}
});
}
};
Has anyone run into this issue?
Thank you in advance.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6
Top Results From Across the Web
Built-in Helpers - Handlebars
Built-in Helpers. # #if. You can use the if helper to conditionally render a block. If its argument returns false , undefined ,...
Read more >Handlebars helper not working with global variable in Express
I have an app running on Node, Express ...
Read more >Handlebars helper reference - Celigo Help Center
Handlebars helpers can be used to implement functionality that is not part of the handlebars language itself. The following list of helpers is...
Read more >Handlebars Helpers Reference - BigCommerce Dev Center
Differs from the get helper in that this helper will return the actual object including the given property key. This helper does not...
Read more >handlebars - Rust - Docs.rs
ok_or(RenderError::new("param not found"))?; out.write("3rd helper: ")?; out.write(param.value().render().as_ref())?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
And in your templates, simply call your global helpers this way:
<div>{{yourFirstGlobalHelper foo }}</div>
I’ve replicated this too.
So for me when I have a an each helper:
{{#each someArray}} {{>partial itemInArray}} {{/each}}
Then in the partial.hbs, custom helpers that have been added to global Express are not available.This also seems to be true for parent context items when in each ( …/parentItem) and there is a bug for this too: #196