Nodejs Express framework in a project using both Ejs template and Jade template _node.js

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.