当前位置:网站首页>Nodejs 模板引擎ejs
Nodejs 模板引擎ejs
2022-07-27 16:20:00 【敢问】
安装ejs cnpm install –-save ejs
基本使用
┣ 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>你好我是<%=names%></h1>
</body>
</html>app.js
var express = require("express");
var app = express()
//设置默认的 模板引擎
app.set("view engine","ejs")
//指的是设置对应的模板引擎文件夹,也就是模板引擎回去固定的文件夹中找ejs文件,默认是views文件夹
app.set("views")
app.get("/",function (req,res) {
res.render("index",{
"names":"张三"
})
})
app.listen(3000,function () {
console.log("监听3000端口")
})

基本使用2
- views文件夹可以改变,使用下面的语句,在第二个参数设置对应的模板文件夹名称,第一个参数必须是views,默认不写第二个参数表示模板引擎文件夹为views
app.set("views","templates");
- 模板的拓展名必须是.ejs结尾的文件,但是在render的时候不需要写拓展名
比如我们建了一个login.ejs文件,在下面进行render

login.ejs
<body>
<h1>大家好我是<%=classes%>的<%=names%>,我考了<%=score%>分</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()
//设置默认模板引擎
app.set("view engine","ejs");
app.set("views","templates")
app.get("/",function (req,res) {
res.render("login",{
"names":"小王",
"classes":"五年级",
"score":"595",
"kemu":["语文","数学","英语","物理","化学","生物"]
})
})
app.listen(3000,function () {console.log("监听3000") })
<%%>表示的是设置模板引擎作用域,内部多用于循环和判断语句
<%=%>表示的是输出语句
边栏推荐
- LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
- 商品评论信息与评论信息分类
- Generate PDM file from Navicat export table
- ridis命令笔记
- TypeError: conv2d(): argument ‘padding‘ (position 5) must be tuple of ints, not str【报错】
- Valueerror: found input variables with inconsistent numbers of samples: [80019456, 26673152] [error reporting]
- Join query and subquery
- Visual studio code installation tutorial (super detailed)
- MySQL set validate_ Adding skip grant tables after password=off failed to start the service
- Multifunctional wireless remote control moxibustion instrument chip dltap703sd
猜你喜欢

JDBC-MySql 02 数据访问和DAO模式

地图找房的实例

面试官:你觉得你最大的缺点是什么?

Bathroom with demister vanity mirror touch chip-dlt8t10s

"MySQL things" explains the indexing principle in detail

Must the MySQL query column be consistent with the group by field?

Ant privacy computing innovation tee technology has won academic recognition

What should I do if MySQL master-slave replication data is inconsistent?

What does the number of network request interface layers (2/3 layers) mean

LeetCode 刷题 第一天
随机推荐
瑞吉外卖笔记
Matplotlib (basic usage)
Visual studio code installation tutorial (super detailed)
Low noise anion fan touch IC
Uni app for wechat login (to be finished)
Login page tablelayout
订单超时取消 及 按类别查询商品
10 SQL optimization schemes summarized by Alibaba P8 (very practical)
网红RGB镜子灯触摸芯片-DLT8S15B-杰力科创
Canvas draws graphics according to coordinate points
filebeat.yml配置文件关于多个服务的配置问题
Conflict between blur event and click event in input box
Arrays and objects in JS
[yuntu said] 249 mobile application security service - app's physical examination center, comprehensive testing, safe on the road!
JDBC-MySql 01 JDBC操作MySql(增删改查)
JS tool - Cookie simple encapsulation
【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
pygame飞机大战游戏背景实现
Was not registered for synchronization because synchronization is not active[resolved]
Uni app label jump