gpt4 book ai didi

javascript - 如何在 .EJS 模板内添加变量

转载 作者:太空宇宙 更新时间:2023-11-04 01:33:40 26 4
gpt4 key购买 nike

我有一个索引页面、一个关于页面和一个联系页面。我还有一个 header.ejs 文件。在 header.ejs 里面我有这个:

<a href="/">Home</a> |
<a href="/about">My Resume</a> |
<a href="contact">My Contact Info</a>
<br>
_______________________________________

-----------------------------------------
<br>


<h3>Copyright 2019 Some text here</h3>

我想对索引页面和联系页面使用完全相同的头文件。我希望每个页面的内容有所不同。该内容将放置在实线和虚线内部。我无法使用单独的头文件。我只能用一个。如何使用相同的模板,但为每个页面创建空间并填充不同的内容?这是我的索引文件的示例:

<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<%include templates/header.ejs%>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
</body>
</html>

最佳答案

在头文件中添加变量page:

<a href="/">Home</a> |
<a href="/about">My Resume</a> |
<a href="contact">My Contact Info</a>
<% if(page=='home') { %>
// add your home page header content here
<% }else if(page=='contact'){%>
// add your contact page header content here
<% }else if(page=='resume'){%>
// add your resume page header content here
<% }else{ %>
// default header
<% } %>
<h3>Copyright 2019 Some text here</h3>

通过传递页面变量来包含它:

主页:

<%- include('templates/header', {page: 'home'}); %>

联系页面:

<%- include('templates/header', {page: 'contact'}); %>

关于javascript - 如何在 .EJS 模板内添加变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com