In some projects, for example, you take on someone else's project and then you don't want to ejs with an egg ache, or you don't want to jade with an egg ache. You do not want to rewrite the previous page, then you may need to introduce a new Ejs or Jade module, you only need to do the following two steps may be able to complete the use of two templates work
1.consolidate.js
CD to project directory:
NPM Install consolidate--save
Open the project's App.js (maybe your name is another)
Take the App.js form as the following code fragment
App.set (' View engine ', ' Jade ');
To
var engines = require (' consolidate ');
App.engine (' Jade ', engines.jade);
App.engine (' HTML ', Engines.ejs);
Or use these
//app.engine (' Jade ', require (' Jade '). __express);
App.engine (' HTML ', require (' Ejs '). RenderFile);
We can restart the project.
2. A little problem
In one of my project A, the code I actually use is
var engines = require (' consolidate ');
App.engine (' Jade ', engines.jade);
App.engine (' HTML ', Engines.ejs);
App.engine (' Jade ', require (' Jade '). __express);
App.engine (' HTML ', require (' Ejs '). RenderFile);
App.set (' View engine ', ' Jade ');
More of the following line
This type of writing can be used in project A, but it is found in another project B that cannot parse the jade template
In Project B, you can use only
var engines = require (' consolidate ');
App.engine (' Jade ', engines.jade);
App.engine (' HTML ', Engines.ejs);
App.set (' View engine ', ' Jade ');
or
//app.engine (' Jade ', require (' Jade '). __express);
App.engine (' HTML ', require (' Ejs '). RenderFile);
How to reference the Ejs template engine in the Express framework
1. How to install the Ejs template engine in a project
Use the following command to establish the basic structure of a Web site in the Nodejs guide:
Express-t Ejs Microblog
Continue running after running this command
CD microblog && npm Install (dependency properties for the Setup project) found that the installed template engine was jade, not ejs. The reason is that the current version has no-t this command, instead
EXPRESS-E microblog
After running this command, continue to run the CD microblog && NPM install,ejs template engine is installed.
But express3 above version of the layout default to cancel, so now in the Views folder does not generate Layout.ejs.
2. After installing the Ejs, how to use the Ejs layout template
Install Express-partials
Switch to the project directory in cmd, run npm install express-partials or
In the Package.json inside the dependencies Add "express-partials": "*". Then run NPM install under the project directory.
Then refer to the express-partials in the App.js, referencing the method:
1. Add Reference to var partials = require (' express-partials ');
2. In App.set (' View engine ', ' Ejs '); Add App.use (partials ()) below;
Call layout where you need to refer to the template: ' Template name ' sample
App.get ('/reg ', function (req, res) {
res.render (' Reg ', {
title: ' User Registration ',
layout: ' Template '
});
The above content to introduce the Nodejs Express framework of a project in the same time use Ejs template and Jade template, I hope you like.