Search code examples
javascriptnode.jshandlebars.jssails.js

Setting Sails js view extension doesn't work


I'm using sails 0.10.5 with handlebars.

I would like all my handlebars templates to use the extension .hbs instead of .handlebars. I read in the sails documentation that you can set an extension property in config/views to change the file extension that sails will look for when rendering views.

http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.views.html

I've set the extension that I would like to use but it's not being used when sails is rendering the view. This is what I get in the browser when going to my index route.

{
  "message": "Could not render view \"index\".  Tried locating view file @ \"/Users/jeff/Sites/bcsm/views/index\".",
  "code": "E_VIEW_FAILED",
  "status": 500,
  "view": {
    "name": "index",
    "root": "/Users/jeff/Sites/bcsm/views",
    "defaultEngine": "handlebars",
    "ext": ".handlebars"
  }
}

This is my config :

module.exports.views = {
  engine: 'handlebars',
  layout: false,
  ext: '.hbs'
};

I've also tried the following with no luck :

extension: 'hbs'
extension: '.hbs'
ext: 'hbs'

Has anyone solved this problem before, or have any ideas?


Solution

  • Ouch, it looks like the docs are a bit off on that one. Thanks for pointing it out.

    In order to use a custom file extension, you'll need to specify a custom view engine in the engine property. To do so for Handlebars, do:

    npm install handlebars
    npm install consolidate
    

    and in config/views.js do:

    engine: {
      ext: 'hbs',
      fn: require("consolidate").handlebars
    }