当前位置:网站首页>Express模板引擎
Express模板引擎
2022-06-28 14:54:00 【抗争的小青年】
与express一起使用的一些流行模板引擎是Pug,Mustache和EJS。Express应用程序默认使用Pug,但他也支持其他几个。
需要在应用中进行如下设置才能让Express渲染模板引擎:
- view,放模板的文件目录,例如:
app.set('views','./views)。 - view engine,要使用的模板引擎。例如,要使用pug模板引擎:
app.set('view engine', 'pug')
在路由中渲染模板
在路由渲染模板并将渲染后的HTML字符串发送到客户端。
res.rander(view [, locals] [, callback])
- view:一个字符串,view是渲染的模板文件的文件路径。
- locals:一个对象,其属性定义视图的局部变量。
router.get("/", (req, res) => {
// res.send({ list: ['aaa', 'bbb', 'ccc'] })
res.render("list", {
title: "新闻", list: ["aa", "bb", "cc"] })
})
ejs模板引擎的使用
安装ejs
npm i ejs
在express配置ejs模板引擎
使用ejs模板引擎
在入口文件中配置如下代码,配置Express使用ejs模板引擎
//配置模板引擎
app.set("views", path.join(__dirname, "./views")) //设置模板引擎的保存位置
app.set('view engine', 'ejs')
注意:
此时指定的模板目录为
views,且模板文件的后缀名为.ejs
设置模板后缀为html
在app.js中添加如下代码,配置Express使用ejs模板引擎。并指定模板后缀名为html。
app.set('views',path.join(__dirname,'views')); //设置模板存储位置
app.set('view engine','html');
app.engine('html',require('ejs').renderFile); //使用ejs模板引擎解析html
注意:此时指定的模板目录为
views,且模板文件的后缀名为.html。
ejs模板语法
<%= %> 输出标签
<%- %> 输出html标签(html会被浏览器解析)
<%# %> 注释标签
<% %> 流程控制标签(写的是if,else,for)
<%- include("header.html",{user:user})%> 导入公共的模板内容
<body>
<%- include("./haeder.html",{isShow:true})%>
这是list页面
<%=title%>
<ul>
<% for(var i=0;i<list.length;i++){ %>
<li>
<%=list[i]%>
</li>
<%} %>
</ul>
<h%- <h5>我是输出的html标签</h5>%>
<%# <h3>注释标签,不会显示在页面结构中</h3>%>
<%- include("./footer.html")%>
</body>

边栏推荐
- 使用LamdbaUpdateWrapper的setSql作用及风险
- 开闭原则
- Setsql function and risk of using lamdbaupdatewrapper
- 名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
- 【黑马早报】腾讯回应大批用户QQ号被盗;薇娅丈夫公司被罚19万;中国恒大被申请清盘;关晓彤奶茶店回应被加盟商起诉...
- 鸟类飞行状态下穿戴式神经信号与行为数据检测记录系统的技术难点总结
- 量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
- Leetcode(665)——非递减数列
- Q-tester 3.2: applicable to development, production and after-sales diagnostic test software
- vscode编写markdown文件并生成pdf
猜你喜欢

Power battery is divided up like this

2022年最新PyCharm激活破解码永久_详细安装教程(适用多版本)

蔚来潜藏的危机:过去、现在到未来

动力电池,是这样被“瓜分”的

After QQ was stolen, a large number of users "died"

Vscode writes markdown file and generates pdf

PMP认证证书的续证费用是多少?

Four visualization tools are recommended to solve 99% of large screen visualization projects!

Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index

竞远安全冲刺创业板:拟募资4亿 启元投资与云游软件是股东
随机推荐
QQ被盗号后群发黄图,大批用户“社死”
【mysql学习笔记24】索引设计原则
蔚来潜藏的危机:过去、现在到未来
How can I get the stock account opening discount link? Is it safe to open a mobile account?
[JS] Fibonacci sequence implementation (recursion and loop)
3. caller service call - dapr
新零售线下店逆势起飞,通膨乌云下的消费热情
Vector explanation + topic
Gas station (greedy)
美因基因港交所上市:市值43亿港元 IPO被市场忽略
Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
The hidden crisis of Weilai: past, present and future
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
[Zhongyi Xinsheng] 5 SPI interface test TF Card
@Controlleradvice + @exceptionhandler handles controller layer exceptions globally
不要使用短路逻辑编写 stl sorter 多条件比较
Classmate Zhang hasn't learned to be an anchor yet
Summary of technical difficulties of wearable neural signal and behavior data detection and recording system for birds in flight
10 key points to effectively improve performance interview
智能化转型被加速,企业需要新的工具箱