[英]Render HTML on Node.js Express (Include HTML code in another HTML file)
我正在使用安裝了Express-Framework的Node.js服務器,並且在合並兩個不同的html文件時遇到問題。
我有一個基本的template.html文件,其中包含<div id="content">
。 現在,我想在將數據發送到客戶端之前在此div中包含content.html。
如何才能做到這一點?
我喜歡使用EJS,因為它是最簡單的模板語言,但是同樣的想法也適用於翡翠或您可能使用的任何東西。 要在運行npm install ejs
在express中啟用此npm install ejs
:
app.engine('.ejs', require('ejs').__express);
發送答復時,運行:
res.render('content.html.ejs');
將您的template.html插入views/header.html.ejs
和views/footer.html.ejs
,然后將它們包含在views/content.html.ejs
如下所示:
<% include header.ejs %>
Content Here
<% include footer.ejs %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.