The Nodejsexpress framework uses both the ejs template and the jade template _ node. js in a project.

Source: Internet
Author: User
This article describes how to use the ejs template and jade template in a node jsexpress framework project. For more information, see some projects, for example, if you take over a project from someone else and you don't want to use egg JS or jade. If you do not want to rewrite the previous page, you may need to introduce an ejs or jade module. You only need to perform the following two steps to use the two templates.

1. Define lidate. js

Cd to the project directory:

Npm install upgrade lidate -- save

Open the app. js of the project (maybe your name is another name)

The code snippet of app. js is as follows:

App. set ('view engine ', 'jade ');

Change

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);app.set('view engine', 'jade'); 

Restart the project.

2. A little problem

In one of my project A, the code I actually use is

Var engines = require ('invalid lidate'); 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'); // The following app line is displayed. set ('view engine ', 'html ');

This method can be used in project A, but the jade template cannot be parsed in another project B.

Project B can only use

Var engines = require ('invalid lidate'); 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); // app. set ('view engine ', 'jade ');

How to reference the ejs template engine in the Express framework

1. How to install the ejs template engine in the project

In the NodeJS guide, use the following command to create the basic structure of the website:

Express-t ejs microblog

Run this command to continue running

Cd microblog & npm install (dependency attribute of the installation project), we found that the installed template engine is jade, not ejs. The reason is that the current version does not have the-t command.

Express-e microblog

After running this command, continue to run cd microblog & npm install. The ejs template engine is installed.

However, layout is canceled by default in versions above express3, so layout. ejs is not generated in the views folder.

2. How to Use the layout template of ejs after ejs is installed

Install express-partials

Switch to the project directory in cmd and run npm install express-partials or

Add "express-partials": "*" to dependencies in package. json ":"*". Run npm install in the project directory.

Then reference express-partials in app. js. Reference Method:

1. Add reference var partials = require ('express-partials ');

2. Add app. use (partials () under app. set ('view engine ', 'ejs ());

Call layout: 'template name' to reference the template.

App. get ('/reg', function (req, res) {res. render ('reg ', {title: 'user registration', layout: 'template '});});

The above content introduces you to the Nodejs express framework. In a project, you can use both ejs templates and jade templates.

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.