Can I use multiple template engines in Sails.js?

Express

In vanilla Express.js, the following code works well.

var app = require('express')(); app.get('/jade', function(req, res) { res.render('slash.jade'); }); app.get('/ejs', function(req, res) { res.render('slash.ejs'); }); app.listen(1338); 

As long as modules are present in node_modules , both templates are displayed by their respective engines.

You can also specify the default engine:

 app.set('view engine', 'haml'); app.get('/', function(req, res) { res.render('slash'); //looks for slash.haml in views directory }); 

In Express, the default view engine is used only when the extension is not specified .

Sails

In Sails.js, it seems that the specified config/view.js is the only one used in it.

If I try to specify the extension directly, I get the following error:

 error: Ignoring attempt to bind route (/barn) to unknown view: barn.jade 

Can I use different viewing mechanisms without a lot of voodoo in Sails?

+5
source share
1 answer

The shortest and most accurate answer is no.

Due to meager boredom, I looked at this question and plunged a little into the code of the viewing engine in sails. If you are interested, you can also find these files in your sails project by going to the directory:

node_modules \ sails \ lib \ hooks \ view

What you find is sails out of the box, configured to use only one viewing mechanism. In the above directory, you will find a file called configure.js, this is where the logic for setting up the custom viewer goes.

Here is a snippet of code

 // Normalize view engine config and allow defining a custom extension if (_.isString(sails.config.views.engine)) { var viewExt = sails.config.views.extension || sails.config.views.engine; sails.config.views.engine = { name: sails.config.views.engine, ext: viewExt }; } // Get the view engine name var engineName = sails.config.views.engine.name || sails.config.views.engine.ext; 

Unfortunately, there is no cycle for installing multiple engines. Sails simply use the engine passed in the sails.config.views.engine parameter and go from there.

+1
source

Source: https://habr.com/ru/post/1240214/


All Articles