Node JS - 使用路由器添加更多页面(ejs 模板)

Node JS - Adding More pages(ejs templates) using routers

我是 Node/EJS 的新手。因此,需要您对创建新路线进行说明。我可以在 Node js 中使用 EJS 模板系统轻松集成静态 html 文件。但是我无法在主体部分内实现路由(使用路由插入另一个模板)。

我的代码:views/index.ejs

<!DOCTYPE html>
<html lang="en">
  <head>
    <% include ./templates/head.ejs %>
 </head>
  <body class="skin-blue">
      <div class="wrapper">
          <% include ./templates/header.ejs %> 
                <section class="content">
                <div class="content-section container">
                    <div class="row">
                           <% include ./contents/aboutus.ejs %> //aboutus page is rendering      
                    </div>
                </div>
            </section>
           <% include ./templates/footer.ejs %>      
      </div>
   <% include ./contents/help-popup.ejs %> 
   <% include ./templates/jsfiles.ejs %> 
  </body>
</html>

这里,显然 aboutus.ejs 在正文部分内正常工作。现在我想通过单击 aboutus.ejs 内的 link 来调用 careers.ejs。页眉和页脚不应更改。如何通过路由添加&渲染careers.ejs?

我想你很期待像JADE这样的布局系统。这可以通过 npm 包 EJS-Locals 来实现。在其中,您可以不调用 ejs 文件,而是提供 HTML.

的正文部分

例如:样板文件

<!DOCTYPE html>
<html>
  <head>
    <title>It's <%=who%></title>
    <%-scripts%>
    <%-stylesheets%>
  </head>
  <body>
    <header>
      <%-blocks.header%>
    </header>
    <section>
      <%-body -%>
    </section>
    <footer>
      <%-blocks.footer%>
    </footer>
  </body>
</html>

aboutus.ejs:

<% layout('boilerplate') -%>
 <% script('foo.js') -%>
 <% stylesheet('foo.css') -%>
 <h1>I am the <%=what%> list </h1>
 <% block('header', "<p>I'm in the header.</p>") -%>
 <% block('footer', "<p>I'm in the footer.</p>") -%>

career.ejs:

<% layout('boilerplate') -%>
<% script('foo.js') -%>
<% stylesheet('foo.css') -%>
<h1>I am <%=what%> Programmer in USA  </h1>
<% block('header', "<p>I'm in the header.</p>") -%>
<% block('footer', "<p>I'm in the footer.</p>") -%>

因此,在此您可以使用 EJS-Locals 包含其他模板。