1

Handlebars template's filename extension

Hi, my templates files got the extension .handlebars and I want to change it to .hbs

const express = require('express');
const exphbs  = require('express-handlebars');

const app = express();

app.engine('handlebars', exphbs({defaultLayout: 'index'}));
app.set('view engine', 'handlebars');

when changing handlebars to hbs and renaming the files, errors appear. The files are not found anymore.

What is missing?

peterHasemann
  • 1,550
  • 4
  • 24
  • 56
  • 1
    [How to change handlebars extension for express?](https://stackoverflow.com/questions/34580640/how-to-change-handlebars-extension-for-express) – Yves Kipondo Nov 28 '17 at 10:13

2 Answers2

2

Try setting the following properties

const exphbs  = require('express-handlebars');
const handlebars = exphbs.create({
  // layoutsDir: path.join(__dirname, 'app/views/layouts'),
  // partialsDir: path.join(__dirname, 'app/views/partials'),
  defaultLayout: 'index',
  extname: 'hbs'
});

app.engine('hbs', handlebars.engine);
app.set('view engine', '.hbs');
Muthukumar
  • 8,679
  • 17
  • 61
  • 86
2

Change your code something like this:

`

const express = require('express');
var app = express();
const hbs = require('express-handlebars');
app.set('views', path.join(__dirname, 'views'));
app.engine('hbs', hbs({extname : 'hbs', defaultLayout: 'index', layoutsDir: __dirname+'/views/layouts'}));
app.set('view engine', 'hbs');

Change your file's extentions from .hanlebars to .hbs and put your index.hbs file inside path/to/project/views/layouts and rest of the .hbs file should remain in path/to/project/views/.

HOTAM SINGH
  • 121
  • 2
  • 11