javascript - 如何在 .EJS 模板中添加变量
问题描述
我有一个索引页面、一个关于页面和一个联系页面。我也有一个 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'}); %>
推荐阅读
- linux - 如何实现与fio一样高的磁盘写入速度?
- c# - 如何在 C# MVC 中的列表中添加对象列表
- ios - 服务器请求 Google Calendar API 使用来自客户端的访问令牌
- ios - 如何使用 iOS Compression Framework 压缩多个文件?
- java - 如何完美隐藏底部导航栏
- jsf - 允许用户以 jsf/adf 形式在日期时间选择器中输入毫秒
- r - 如何将 2 列矩阵转换为数据框表
- amazon-web-services - AWS cognito 机器对机器 oauth
- css - 有角度的 CSS 线性渐变在 Firefox 和边缘中不平滑
- gcloud - 如何获取 gcloud composer 命令的输出?