当前位置:网站首页>Nodejs template engine EJS
Nodejs template engine EJS
2022-07-27 18:58:00 【venture to ask】
install ejs cnpm install –-save ejs
Basic use
┣ views
│┣ index.ejs
┣ app.js
index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1> Hello, I am <%=names%></h1>
</body>
</html>app.js
var express = require("express");
var app = express()
// Set default template engine
app.set("view engine","ejs")
// It refers to setting the corresponding template engine folder , That is, the template engine goes back to the fixed folder to find ejs file , The default is views Folder
app.set("views")
app.get("/",function (req,res) {
res.render("index",{
"names":" Zhang San "
})
})
app.listen(3000,function () {
console.log(" monitor 3000 port ")
})

Basic use 2
- views Folders can be changed , Use the following statement , Set the corresponding template folder name in the second parameter , The first parameter must be views, The second parameter is not written by default, which means that the template engine folder is views
app.set("views","templates");
- The extended name of the template must be .ejs Final document , But in render You don't need to write the extension name
For example, we built a login.ejs file , Proceed below render

login.ejs
<body>
<h1> Hello, I'm <%=classes%> Of <%=names%>, I took the test <%=score%> branch </h1>
<ul>
<% for(var i=0; i<kemu.length; i++) { %>
<li><%=kemu[i]%></li>
<% } %>
</ul>
</body>app.js
var express = require("express");
var app = express()
// Set the default template engine
app.set("view engine","ejs");
app.set("views","templates")
app.get("/",function (req,res) {
res.render("login",{
"names":" Xiao Wang ",
"classes":" Fifth grade ",
"score":"595",
"kemu":[" Chinese language and literature "," mathematics "," English "," Physics "," chemical "," biological "]
})
})
app.listen(3000,function () {console.log(" monitor 3000") })
<%%> It means setting the scope of the template engine , Internally, it is mostly used for loops and judgment statements
<%=%> It means output statement
边栏推荐
- 用Matlab生成适用于期刊及会议的图形- plot
- How to send external mail to the company mailbox server on the Intranet
- LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
- Collection of software design suggestions of "high cohesion and low coupling"
- Use mobaxtermto establish a two-tier springboard connection
- Here are all the MySQL interview questions you can't expect (the latest version of 2022)
- Intelligent insomnia therapeutic instrument product dlt8p68sa Jericho
- idea 2020.1社区版下载体验
- EN 1155 building hardware swing door opener - CE certification
- Interceptor interceptor
猜你喜欢
随机推荐
面试官:你觉得你最大的缺点是什么?
Mini washing machine touch chip dlt8ma12ts Jericho
Zhaoqi scientific and technological innovation introduces high-level talents at home and abroad and connects innovation and entrepreneurship projects
Set the arc button to be displayed in the middle
How to send external mail to the company mailbox server on the Intranet
Ridis command notes
EN 1155 building hardware swing door opener - CE certification
Latex使用-控制表格或者图形的显示位置
Idea 2020.1 Community Edition download experience
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
Household mute mosquito repellent lamp chip-dltap703sd-jericho
MongoDB
Overview of Baidu map technology, and application development of basic API and webapi
Acquisition data transmission mode and online monitoring system of vibrating wire wireless acquisition instrument for engineering instruments
Here are all the MySQL interview questions you can't expect (the latest version of 2022)
正则表达式的扩展
WPS turns off annoying advertisements
浅谈JVM(面试常考)
LeetCode 刷题 第一天
C basic concepts list description suggestions collection








