节点JS – 使用路由器添加更多页面(ejs模板)

我是Node / EJS的新手。 所以需要您澄清创build新的路线。 我可以很容易地在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里面的链接来调用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包含其他模板。