当前位置:网站首页>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>
边栏推荐
- What are the benefits of this PMP certificate?
- 物联网低代码平台常用《组件介绍》
- 证券公司和银行哪个更安全 怎么办理开户最安全
- Differences between ram ROM flash
- 抽奖动画 - 鲤鱼跳龙门
- Kwai investment e-commerce service provider Yixin optimization
- 不要使用短路逻辑编写 stl sorter 多条件比较
- Calculator (force buckle)
- 哪个证券公司最大最安全 怎么办理开户最安全
- [MySQL learning notes 24] index design principles
猜你喜欢
5000倍回报,南非报业投资腾讯赚了一个省
币圈大地震:去年赚100万,今年亏500万
Jackie Chan and fast brand, who is the Savior of Kwai?
Technical trendsetter
美因基因港交所上市:市值43亿港元 IPO被市场忽略
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
Softing epGate PB系列网关-可将PROFIBUS总线集成到EtherNet/IP网络
3. Caller 服务调用 - dapr
Is PMP really useful?
Construction and management practice of ByteDance buried point data flow
随机推荐
5000倍回报,南非报业投资腾讯赚了一个省
R语言ggplot2可视化:patchwork包将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、两个可视化的组合结果对齐、并为组合图像的每个子图添加标题
智能化转型被加速,企业需要新的工具箱
Force deduction solution summary 522- longest special sequence II
币圈大地震:去年赚100万,今年亏500万
从莫高窟到太平洋,海量数据找到了新家园
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
不要使用短路逻辑编写 stl sorter 多条件比较
How to solve the following problems in the Seata database?
优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿
Vscode writes markdown file and generates pdf
完整的模型训练套路(一)
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
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和数据表格横向组合起来形成最终结果图
雷科防务:4D毫米波雷达产品预计可以在年底量产供货
【黑马早报】腾讯回应大批用户QQ号被盗;薇娅丈夫公司被罚19万;中国恒大被申请清盘;关晓彤奶茶店回应被加盟商起诉...
The latest pycharm activation cracking code in 2022 is permanent_ Detailed installation tutorial (applicable to multiple versions)
Technical trendsetter
环保产品“绿色溢价”高?低碳生活方式离人们还有多远