Edit Page

View engines

The default view engine in Sails is EJS.

Swapping out the view engine
#

To use a different view engine, you should use npm to install it in your project, then in config/views.js set sails.config.views.extension to your desired file extension and sails.config.views.getRenderFn to a function that returns your view engine's rendering function.

If your view engine is supported by Consolidate, you can use that in getRenderFn to easily access the rendering function. First, you'll need to use npm to install consolidate into your project, if it is not already present:

npm install consolidate --save

After the install has completed and you have installed your view engine package, you can then set the view configuration. For example, to use Swig templates you would npm install swig --save and then add the following into config/views.js:

extension: 'swig',
getRenderFn: ()=>{
  // Import `consolidate`.
  var cons = require('consolidate');
  // Return the rendering function for Swig.
  return cons.swig;
}

The getRenderFn allows you to configure your view engine before plugging it into Sails:

extension: 'swig',
getRenderFn: ()=>{
  // Import `consolidate`.
  var cons = require('consolidate');
  // Import `swig`.
  var swig = require('swig');
  // Configure `swig`.
  swig.setDefaults({tagControls: ['{?', '?}']});
  // Set the module that Consolidate uses for Swig.
  cons.requires.swig = swig;
  // Return the rendering function for Swig.
  return cons.swig;
}

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Concepts