简体   繁体   English

将 ejs 或 header 和页脚模板用于 static 托管与 github 页面?

[英]Use ejs or header and footer templates for static hosting with github pages?

I have around 10 html documents that only differ due to one div.我有大约 10 个 html 文档,它们仅因一个 div 而不同。 Is there any way to make a header and footer document, and link them together for each html document?有什么方法可以制作 header 和页脚文档,并将每个 html 文档链接在一起? I have used EJS in the past, so I would prefer using it.我过去使用过 EJS,所以我更喜欢使用它。 I am using github pages to host my site, so I cannot use any backend.我正在使用 github 页面来托管我的网站,所以我不能使用任何后端。

Yup, These are called partials in EJS, and you insert them with an include function:是的,这些在 EJS 中被称为部分,您可以使用include function 插入它们:

<%- include('header') %>

<!-- Main content -->

<%- include('footer') %>

EJS will look for them in the local directory. EJS 将在本地目录中查找它们。 Any parameters you passed into app.render in your Express route are passed down to partials too.您在 Express 路由中传递给app.render的任何参数也将传递给 partials。

If you need to re-use partials on the same page with unique data, you can pass them a data object too:如果您需要在具有唯一数据的同一页面上重复使用部分,您也可以将数据 object 传递给它们:

<% msgs.forEach((msg) => { %>
    <%- include('msgPartial', { msg: msg }) %>
<% }) %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM