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

边栏推荐
- 张同学还没学会当主播
- 环保产品“绿色溢价”高?低碳生活方式离人们还有多远
- 当下不做元宇宙,就像20年前没买房!
- Leetcode (406) - rebuild the queue based on height
- 快手投资电商服务商易心优选
- Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market
- Jackie Chan and fast brand, who is the Savior of Kwai?
- 解决Unable to create process using ‘D:\Program File
- code snippet
- 蔚来潜藏的危机:过去、现在到未来
猜你喜欢

Angers medical sprint scientific innovation board: annual revenue of RMB 300million and proposed fund raising of RMB 770million

Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan

成龙和快品牌,谁才是快手的救星?

Power battery is divided up like this

美国乔布斯,殁了;中国乔布斯,卖了

【黑马早报】腾讯回应大批用户QQ号被盗;薇娅丈夫公司被罚19万;中国恒大被申请清盘;关晓彤奶茶店回应被加盟商起诉...
![[spatial & single cellomics] phase 1: Study on PDAC tumor microenvironment by single cell binding spatial transcriptome](/img/c4/c053ec830a05f22cab7f7381f9f2b3.png)
[spatial & single cellomics] phase 1: Study on PDAC tumor microenvironment by single cell binding spatial transcriptome

Q-tester 3.2: applicable to development, production and after-sales diagnostic test software

Jackie Chan and fast brand, who is the Savior of Kwai?

物联网低代码平台常用《组件介绍》
随机推荐
Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market
蔚来潜藏的危机:过去、现在到未来
3. Caller 服务调用 - dapr
美国乔布斯,殁了;中国乔布斯,卖了
美因基因港交所上市:市值43亿港元 IPO被市场忽略
sent2vec教程
证券公司和银行哪个更安全 怎么办理开户最安全
New offline retail stores take off against the trend, and consumption enthusiasm under the dark cloud of inflation
[C language] nextday problem
[C language] implementation of binary tree and three Traversals
spacy教程(持续更新ing...)
Softing epgate Pb series Gateway - integrates the Profibus bus into the ethernet/ip network
名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
10 key points to effectively improve performance interview
BatchNorm2d原理、作用及其pytorch中BatchNorm2d函数的参数讲解
The latest pycharm activation cracking code in 2022 is permanent_ Detailed installation tutorial (applicable to multiple versions)
Does Frankfurt currently support SQL?
How to solve the following problems in the Seata database?
open3d里pointcloud和numpy数组之间的转化