当前位置:网站首页>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") })
<%%>表示的是设置模板引擎作用域,内部多用于循环和判断语句
<%=%>表示的是输出语句
边栏推荐
猜你喜欢

LeetCode 刷题 第三天

Knowledge map pyhanlp realizes named body recognition (with named body recognition code)

Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)

Whole body multifunctional massage instrument chip-dltap602sd

Vue uses keep alive to realize page caching

Login page tablelayout

Build a simple knowledge question and answer system

Intelligent insomnia therapeutic instrument product dlt8p68sa Jericho

阿里架构师耗时280个小时整理的1015页分布式全栈小册,轻松入手分布式系统

Ant privacy computing innovation tee technology has won academic recognition
随机推荐
Runtimeerror: output with shape [1, 256, 256] doesn't match the broadcast shape [3, 256, 256] [error]
MySQL 04 高级查询(二)
"MySQL things" explains the indexing principle in detail
LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
I'm stupid. When completable future is used with openfegin, it even reports an error
Part of speech list of common words
Whole body multifunctional massage instrument chip-dltap602sd
Functions in JS and the use of DOM to obtain elements and event attributes
Visual studio code installation tutorial (super detailed)
Redis注解
LED带风扇护眼学习台灯触摸芯片-DLT8S12A
JS to realize simple form verification and select all functions
Uploading and downloading of files
阿里架构师耗时280个小时整理的1015页分布式全栈小册,轻松入手分布式系统
Generate PDM file from Navicat export table
Hbuilder submission code
Arrays and objects in JS
浴室带除雾化妆镜触摸芯片-DLT8T10S
【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
Baidu map eagle eye track service