当前位置:网站首页>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>
边栏推荐
- 开闭原则
- 【黑马早报】腾讯回应大批用户QQ号被盗;薇娅丈夫公司被罚19万;中国恒大被申请清盘;关晓彤奶茶店回应被加盟商起诉...
- Performance comparison of deep learning models on cat and dog image data sets
- Kwai investment e-commerce service provider Yixin optimization
- Rails advanced -- framework theory cognition and construction scheme construction (I)
- The time schedule for the soft test in the second half of 2022 has been determined!
- How does Seata server 1.5.0 support mysql8.0?
- What are the benefits of this PMP certificate?
- 动力电池,是这样被“瓜分”的
- 哪个证券公司最大最安全 怎么办理开户最安全
猜你喜欢
Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?
openGauss内核:SQL解析过程分析
Q-Tester 3.2:适用于开发、生产和售后的诊断测试软件
Introduction to common components of IOT low code platform
The hidden crisis of Weilai: past, present and future
The time schedule for the soft test in the second half of 2022 has been determined!
Le patron a donné trois ordres: discret, discret, discret
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿
随机推荐
法兰克福地区目前支持sql了吗?
股票开户优惠链接,我如何才能得到?手机开户是安全么?
Practice of constructing ten billion relationship knowledge map based on Nebula graph
华泰证券开户有什么后果 怎么办理开户最安全
Leetcode(665)——非递减数列
[C language] how to generate normal or Gaussian random numbers
Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
机器人的运动范围(DFS)
Combined sum leetcode
考了这个PMP证书到底有什么好处?
dolphinscheduler2. Installation of X (valid for personal test)
Gas station (greedy)
抽奖动画 - 鲤鱼跳龙门
After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time
组合总和-Leetcode
2022 operation of simulation test platform for 100 simulated questions of main principals of metal and nonmetal mines (underground mines)
Robot range of motion (DFS)
seata-server 1.5.0 如何支持mysql8.0?
Does Frankfurt currently support SQL?
Flutter series: offstage in flutter