EJS / Node / Express-具有部分标题,如何更改每个页面的标题?

时间:2018-09-09 13:17:05

标签: node.js express ejs

我最近参加了有关后端Web开发的课程。

正如标题所述,我不知道如何为整个网站使用部分标题,但是每个页面都有不同的标题。 (因为包含在代码中)

有什么把戏吗?

2 个答案:

答案 0 :(得分:0)

包括部分内容的每个页面可以自由地通过参数将数据传递给所述部分内容。有关示例,请参见here

答案 1 :(得分:0)

1st)在您的Express路线中:

app.get("/", function(req, res){
  res.locals.title = "Abel's Home Page";                    // THIS LINE IS KEY
  res.render("home.ejs");
});

2nd)在您的views/home.ejs文件中:

<% include partials/header.ejs %>

3rd)在您的views/partials/header.ejs文件中:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title><%= title %></title>                               <!-- THIS LINE IS KEY -->
  <link rel="stylesheet" href="../style.css">
  <link rel="shortcut icon" href="../logo_sm.png" type="image/x-icon">
</head>
<body>
相关问题