Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .html file extensions for handlebars in express?

So I was wondering how I could use .html extensions instead of .handlebars or .hbs extensions. I am doing this so I can develop using regular html so that my frontend developers can seamless edit files in their IDEs without any extra configuration. Plus it will help installing html templates much faster into our express applications.

like image 813
jemiloii Avatar asked Mar 09 '14 03:03

jemiloii


2 Answers

So I was able to do this by changing three things in my app.js file I hope this helps everyone out as much as it helped me!

var express  = require('express'),
    exphbr   = require('express3-handlebars'), // "express3-handlebars"
    helpers  = require('./lib/helpers'),

    app = express(), 
    handlebars;

// Create `ExpressHandlebars` instance with a default layout.
handlebars = exphbr.create({
    defaultLayout: 'main',
    helpers      : helpers,
    extname      : '.html', //set extension to .html so handlebars knows what to look for

    // Uses multiple partials dirs, templates in "shared/templates/" are shared
    // with the client-side of the app (see below).
    partialsDir: [
        'views/shared/',
        'views/partials/'
    ]
});

// Register `hbs` as our view engine using its bound `engine()` function.
// Set html in app.engine and app.set so express knows what extension to look for.
app.engine('html', handlebars.engine);
app.set('view engine', 'html');

// Seperate route.js file
require("./routes")(app, express);

app.listen(3000);
like image 186
jemiloii Avatar answered Sep 21 '22 23:09

jemiloii


Agree with JemiloII,

  1. extname: '.myext' in the config while creating the expr-HBS instance (exphbr.create()) according to https://www.npmjs.org/package/express3-handlebars#-extname-handlebars-
  2. binding the expr-HBS engine to the extension: app.engine('myext', handlebars.engine); according to http://expressjs.com/3x/api.html#app.engine
  3. set the extension as view engine: app.set('view engine', 'myext'); - unfortunately no link to how it works.

Regards

like image 20
Rom Avatar answered Sep 21 '22 23:09

Rom