I am new to node.js and I'd like to use handlebars template engine but with a shorten extension hbs
.
Here is the original code (source):
var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
It worked fine with templates with handlebars
extension, but I changed it to:
var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main' });
app.engine('hbs', handlebars.engine);
app.set('view engine', 'hbs');
And change all the template extentions to hbs
.
However, now I get this error:
Error: ENOENT: no such file or directory, open '/path/to/node/myproject/views/layouts/main.handlebars'
at Error (native)
I also tried
var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main' , extname : '.hbs'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
as per answer here,
but I results in:
Error: Failed to lookup view "500" in views directory "/path/to/myproject/views"
at EventEmitter.render (/path/to/myproject/node_modules/express/lib/application.js:579:17)
at ServerResponse.render (/path/to/myproject/node_modules/express/lib/response.js:961:7)
at /path/to/myproject/app.js:96:6
at Layer.handle_error (/path/to/myproject/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/path/to/myproject/node_modules/express/lib/router/index.js:310:13)
at /path/to/myproject/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/path/to/myproject/node_modules/express/lib/router/index.js:330:12)
at next (/path/to/myproject/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/path/to/myproject/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/path/to/myproject/node_modules/express/lib/router/index.js:310:13)
I tried other things too but none worked so wondering how can I fix this?
To use handlebars in express, we need to store HTML code into a . hbs extension in the 'views' folder in the source directory as hbs looks for the pages in the views folder. Now, we need to change the default view engine. Now, we render our webpage through express to the local server.
Here's what's going on, Handlebars takes a static template file you give it inside the layouts folder (the “index. handlebars” file), this file contains some kind of a placeholder (the {{{body}}} if you look back at the code) which will be filled with the content of another .
To create the HBS file, customize your settings, select File → Save Setlist As..., name the file, choose the save location, and click Save. To open the H5S file, double-click the file or select File → Open Setlist..., navigate to the file, and click Open. NOTE: The HBS extension was replaced by .
EJS Is Way Faster Than Jade And Handlebars. EJS Has A Really Smart Error Handling Mechanism Built Right Into It. It Points Out To You, The Line Numbers On Which An Error Has Occurred So That You Don't End Up Looking Through The Whole Template File Wasting Your Time In Searching For Bugs.
This did the trick:
exphbs = require('express3-handlebars'),
app.engine('hbs', exphbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', 'hbs');
You can use express-hbs instead of express3-handlebars
.
Simply, you can do:
var hbs = require('express-hbs');
/*
...
*/
app.engine('hbs', hbs.express4({
partialsDir : __dirname +'/views/partials',
defaultLayout : __dirname +'/views/layouts/default',
extname : '.hbs',
layoutsDir : __dirname +'/views/layouts',
}));
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With