Handlebars Helpers issue
See original GitHub issueSo I have written a task in Gulp that’ll go through my Handlebars directory & generate static HTML using JSON files in the project but for some reason, it doesn’t seem to recognise the helpers file I have specified?
var dataSrc = 'src/data';
var handlebarsSrc = 'src/handlebars';
var pageSrc = handlebarsSrc + '/pages';
var partialSrc = handlebarsSrc + '/partials';
var helpersFile = jsApp + 'handlebarsHelpers.js';
var htmlDest = 'dist';
gulp.task('handlebars', function () {
gulp.src(pageSrc + '/**/*.hbs')
.pipe(handlebars({
data: dataSrc + '/index.json',
helpers: helpersFile,
partials: partialSrc + '/**/*.hbs',
bustCache: true,
debug: 1
}))
.pipe(extname('.html'))
.pipe(htmlmin({
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
removeStyleLinkTypeAttributes: true,
removeScriptTypeAttributes: true,
useShortDoctype: true
}))
.pipe(entityconvert({type: 'html'}))
.pipe(gulp.dest(htmlDest))
});
And my Handlebars Helpers file currently looks like this.
/**
* Downloadable Content
* Returns 'has downloadable content' if value of 1 is passed to it.
***/
Handlebars.registerHelper('downloadable_content', function (flag) {
if (flag === 1) {
return '<h4>+ downloadable content</h4>';
} else {
return '';
}
});
/**
* Price Formatter
* Has a price passed into it and then returns a formatted text version of it
***/
Handlebars.registerHelper('price_formatter', function (price) {
if (price === 0) {
return 'Free';
} else {
return '£' + price;
}
});
/**
* Size Formatter
* Has a value passed to it and returns back a formatted version of it.
***/
Handlebars.registerHelper('size_formatter', function (size) {
if(size > 1024) {
return size / 1000 + 'gb';
} else {
return size + 'mb';
}
});
I used debug to try and work out if it was recognise my helpers and it wasn’t.
[16:40:58] Rendering 'index.hbs' with...
[16:40:58] data -> index
[16:40:58] context -> file index
[16:40:58] helpers -> blockHelperMissing each helperMissing if log lookup unless with
[16:40:58] partials -> global/head global/scripts layouts/layout001 layouts/layout002 layouts/layout003 layouts/layout004 layouts/layout005
[16:40:58] Rendering 'articles/article001.hbs' with...
[16:40:58] data -> index
[16:40:58] context -> file index
[16:40:58] helpers -> blockHelperMissing each helperMissing if log lookup unless with
[16:40:58] partials -> global/head global/scripts layouts/layout001 layouts/layout002 layouts/layout003 layouts/layout004 layouts/layout005
events.js:160
throw er; // Unhandled 'error' event
^
Error: Missing helper: "price_formatter"
at Object.<anonymous> (/Users/***.****/PhpstormProjects/*****/prototype/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Issues · helpers/handlebars-helpers - GitHub
188 handlebars helpers in ~20 categories. Can be used with Assemble, Ghost, YUI, express.js etc. - Issues · helpers/handlebars-helpers.
Read more >Built-in Helpers - Handlebars
Helpers are the proposed way to add custom logic to templates. You can write any helper and use it in a sub-expression. For...
Read more >Problem with express-handlebars section helpers
I can't make the section helper work as intended. The body of login.hbs was parsed normally, but the js section was never parsed....
Read more >Helpers in Handlebars.java
It wont let you add logic in the HTML but you can add as much logic as you need through helpers. Want to...
Read more >Handlebars - helper - Themes - Ghost Forum
Same thing, its a bit of an old topic, but since its not detailed in the documentation, here's a way to do it....
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
Ah ok then Shannon. I’ve reverted it back, added that fix as well as discovered an error in the filepath saved in the ‘helpersFile’ variable and it works now, thank you very much!
Awesome! Glad I could help!