Use the template engine _ javascript skills in Express

Source: Internet
Author: User
This article describes how to use the template engine in Express. For more information about how to use the template engine, see the following settings in the application to make the Express rendering template file:

Views: Directory of the template file, for example, app. set ('View', './view ')
View engine, template engine, for example: app. set ('View engine ', 'Jade ')

Then install the corresponding template engine npm package.

$ npm install jade --save

A template engine compatible with Express, such as Jade, uses res. render () to call its export method _ express (filePath, options, callback) to render the template.

Some Template engines do not follow this convention. Consolidate. js can map all popular template engines in Node to this convention, so that they can be seamlessly connected with Express.

Once the view engine is set successfully, you do not need to explicitly specify the engine or load the template engine module in the application. Express has been loaded internally, as shown below.

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

Generate a jade template file named index. Jade under the views Directory. the content is as follows:

html head  title!= title body  h1!= message

Create a route rendering index. jade file. If view engine is not set, you must specify the suffix of the view file. Otherwise, it will be omitted.

app.get('/', function (req, res) { res.render('index', { title: 'Hey', message: 'Hello there!'});});

In this case, the request "index. jade" is rendered as HTML.

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.