当前位置:网站首页>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>

边栏推荐
- Validate palindrome string
- 动力电池,是这样被“瓜分”的
- 哪个证券公司最大最安全 怎么办理开户最安全
- Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion
- IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力
- JS judge whether the string is empty or not
- 运行近20年,基于Win 98的火星探测器软件迎来首次升级
- Do not use short circuit logic to write STL sorter multi condition comparison
- 不要使用短路逻辑编写 stl sorter 多条件比较
- Leetcode(167)——两数之和 II - 输入有序数组
猜你喜欢

Opengauss kernel: analysis of SQL parsing process

IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力

快手投资电商服务商易心优选
![[C language] nextday problem](/img/7b/422792e07dd321e3a37c1fff55c0ca.png)
[C language] nextday problem
Technical trendsetter

PMP真的有用吗?

腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元

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

Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion

Flutter series: offstage in flutter
随机推荐
What are the consequences of opening an account with Huatai Securities? How to open an account is the safest
2022 operation of simulation test platform for 100 simulated questions of main principals of metal and nonmetal mines (underground mines)
Softing epGate PB系列网关-可将PROFIBUS总线集成到EtherNet/IP网络
开闭原则
量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
Seata数据库中出现以下问题要怎么解决呀?
法兰克福地区目前支持sql了吗?
股票开户优惠链接,我如何才能得到?手机开户是安全么?
组合总和-Leetcode
证券公司和银行哪个更安全 怎么办理开户最安全
坐拥1200亿,她又要IPO敲钟了
从小小线虫谈起——溯源神经系统进化,开启生命模拟
dolphinscheduler2.X的安装(亲测有效)
鸟类飞行状态下穿戴式神经信号与行为数据检测记录系统的技术难点总结
[MySQL learning notes 24] index design principles
@Controlleradvice + @exceptionhandler handles controller layer exceptions globally
计算器(力扣)
324. 摆动排序 II : 不简单的构造题
Setsql function and risk of using lamdbaupdatewrapper
币圈大地震:去年赚100万,今年亏500万