peterHasemann
peterHasemann

Reputation: 1580

Changing the Handlebars extensions in NodeJs

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?

Upvotes: 1

Views: 850

Answers (2)

HOTAM SINGH
HOTAM SINGH

Reputation: 121

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/.

Upvotes: 2

Muthukumar
Muthukumar

Reputation: 9579

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');

Upvotes: 2

Related Questions