当前位置:网站首页>Express template engine
Express template engine
2022-06-28 15:02:00 【Struggling young man】
And express Some of the popular template engines used together are Pug,Mustache and EJS.Express The application uses... By default Pug, But he also supports several others .
The following settings need to be made in the application to make Express Render template engine :
- view, The file directory where the template is placed , for example :
app.set('views','./views). - view engine, Template engine to use . for example , To use pug template engine :
app.set('view engine', 'pug')
Render template in route
Route the rendering template and render the rendered HTML The string is sent to the client .
res.rander(view [, locals] [, callback])
- view: A string ,view Is the file path of the rendered template file .
- locals: An object , Its properties define the local variables of the view .
router.get("/", (req, res) => {
// res.send({ list: ['aaa', 'bbb', 'ccc'] })
res.render("list", {
title: " Journalism ", list: ["aa", "bb", "cc"] })
})
ejs Use of template engine
install ejs
npm i ejs
stay express To configure ejs template engine
Use ejs template engine
Configure the following code in the entry file , To configure Express Use ejs template engine
// Configure template engine
app.set("views", path.join(__dirname, "./views")) // Set the save location of the template engine
app.set('view engine', 'ejs')
Be careful :
In this case, the specified template directory is
views, And the suffix of the template file is.ejs
Set the template suffix to html
stay app.js Add the following code to , To configure Express Use ejs template engine . And specify the template suffix as html.
app.set('views',path.join(__dirname,'views')); // Set the template storage location
app.set('view engine','html');
app.engine('html',require('ejs').renderFile); // Use ejs Template engine parsing html
Be careful : In this case, the specified template directory is
views, And the suffix of the template file is.html.
ejs Template syntax
<%= %> Output label
<%- %> Output html label (html It will be parsed by the browser )
<%# %> Comment tags
<% %> Process control label ( Is written if,else,for)
<%- include("header.html",{user:user})%> Import public template content
<body>
<%- include("./haeder.html",{isShow:true})%>
This is a list page
<%=title%>
<ul>
<% for(var i=0;i<list.length;i++){ %>
<li>
<%=list[i]%>
</li>
<%} %>
</ul>
<h%- <h5> I am exporting html label </h5>%>
<%# <h3> Comment tags , Does not appear in the page structure </h3>%>
<%- include("./footer.html")%>
</body>

边栏推荐
- 叮!Techo Day 腾讯技术开放日如约而至!
- 技术弄潮儿
- [C language] implementation of binary tree and three Traversals
- 证券公司和银行哪个更安全 怎么办理开户最安全
- spacy教程(持续更新ing...)
- Rails进阶——框架理论认知与构建方案建设(一)
- Vector explanation + topic
- 哪个证券公司最大最安全 怎么办理开户最安全
- [MySQL learning notes 23] index optimization
- With 120billion yuan, she will ring the bell for IPO again
猜你喜欢

叮!Techo Day 腾讯技术开放日如约而至!

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

Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform

张同学还没学会当主播

5000倍回报,南非报业投资腾讯赚了一个省

环保产品“绿色溢价”高?低碳生活方式离人们还有多远

3. caller service call - dapr

PMP真的有用吗?

安杰思医学冲刺科创板:年营收3亿 拟募资7.7亿

The latest pycharm activation cracking code in 2022 is permanent_ Detailed installation tutorial (applicable to multiple versions)
随机推荐
Introduction to common components of IOT low code platform
证券公司和银行哪个更安全 怎么办理开户最安全
运行近20年,基于Win 98的火星探测器软件迎来首次升级
Express模板引擎
Conversion between pointcloud and numpy arrays in open3d
324. 摆动排序 II : 不简单的构造题
spacy教程(持续更新ing...)
WSUS客户端访问服务端异常报错-0x8024401f「建议收藏」
组合总和-Leetcode
Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
How does Seata server 1.5.0 support mysql8.0?
Seata数据库中出现以下问题要怎么解决呀?
使用LamdbaUpdateWrapper的setSql作用及风险
Leetcode(88)——合并两个有序数组
Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
Rails advanced -- framework theory cognition and construction scheme construction (I)
sent2vec教程
Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
物联网低代码平台常用《组件介绍》