Express using HTML templates

Source: Internet
Author: User

Express uses the Jade template by default and can be configured to support the use of Ejs or HTML templates.

1. Installing Ejs

Install Ejs in the project root directory.

NPM Install Ejs

2. Introduction of Ejs

var ejs = require ('ejs');  // I am the newly introduced Ejs plugin

3. Set the HTML engine

App.engine (' HTML ', ejs.__express);

Setting up the View engine

App.set('view engine'html');

The HTML file can be accessed by restarting the service after saving.

Note : In the Express server, the HTML engine is not configured to add directly, the view engine is configured, the configuration can be modified.

--------------------------------------------------------------------------------------------------------------- ------------------

What do you do to modify these settings?

Why does the view engine have to be modified to add an HTML engine?

Let's take a look at the. Engine () this method.

app.engine (ext, callback);

Express uses the Jade template by default. If you try to load the "Foo.jade" file, the following action is called inside Express.

App.engine ('Jade', Require ('Jade'). __express);

If you want to use a different template engine, such as: Map the Ejs template to the. html file:

App.engine ('html', require ('ejs'). __express);

In this line of code, the Ejs. RenderFile () method is actually called, and Ejs.__express is another name within the Ejs of the method.

The same method is called after the template engine is loaded. __express, so if you are using the Ejs template, you do not have to configure the item.

Summary : Using HTML templates, you need to add app.engine (' HTML ', require (' Ejs '). __express);

Use the Ejs template without configuring the item.

If you create a index.html file or a Index.ejs file in the Views folder, the default Index.jade file is still accessed. What is this for? Here is the second set of settings mentioned above App.set (' View engine ', ' html ');

App.set (name, value);

In the parameters of the. Set () method, one of the items is ' view engine ', which means that the engine plug-in is used by default when no file template format is specified. If this is set to an HTML file, the corresponding HTML file will be found only if the file name is set for the route specified. In this, the brain hole big open, tried a bit in views to create 3 files Test.jade, Test.ejs, test.html. The routing settings are as follows. Access OK! Each route points to the corresponding file. Of course, this kind of writing is not recommended at all and does not conform to reality.

Router.Get('/test/', Function (req, res, next) {Res.render ('Test', {title:'HTML'});}); Router.Get('/test1/', Function (req, res, next) {Res.render ('Test.ejs', {title:'EJS'});}); Router.Get('/test2/', Function (req, res, next) {Res.render ('Test.jade', {title:'Jade});});

Look at the Express API, look at the English document, that a few simple words also pondered several times to understand (suddenly to the technical translator's worship added several grades), can only according to their own understanding to write.

Official Api:http://www.expressjs.com.cn/4x/api.html#app.engine

Express using HTML 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.