Home > Article > Web Front-end > Detailed explanation of using ejs template engine example in express site
This article mainly introduces to you the use of ejs template engine in express sites. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.
The express site created using vs uses the jade template engine by default, but I don’t like this method very much because I think the semantic characteristics of html itself are what I like, and the html itself is also It is concise enough, and using html itself as the template language is more in line with my preferences, so I choose ejs.
1. Installation
Start the console in the root directory of the website and enter
npm install ejs
#The program package will be installed in the node_modules directory.
2 Modify APP.JS
Modify app.js and change the view engine to ejs. (And change the suffix of the template to .html)
app.set('views',path.join(__dirname , 'views') ); app.engine('.html', require('ejs').__express); app.set('view engine', 'html');
3 Create a test page
Create index.html in the views folder, The content is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Pivotal Search</title> <meta name="description" content=""> <meta name="author" content=""> <!-- HTML5 shim, for IE6-8 support of HTML elements --> <!--[if lt IE 9]> <script src="http://apps.bdimg.com/libs/html5shiv/r29/html5.min.js"></script> <![endif]--> <!-- styles --> <link href="css/style.css" rel="external nofollow" rel="stylesheet"> </head> <body> <%= title %> </body> </html>
Then modify index.js in the router:
/* GET home page. */ router.get('/', function(req, res, next) { res.render('index', { title: '测试11111' }); });
Related recommendations:
Node.js template engine Jade detailed explanation
react back-end rendering template engine noox release usage method
Simple js template engine writing method
The above is the detailed content of Detailed explanation of using ejs template engine example in express site. For more information, please follow other related articles on the PHP Chinese website!