2016-01-27 47 views
1

我是Node/EJS的新手。所以需要您澄清创建新路线。我可以在Node js中使用EJS模板系统轻松集成静态html文件。但是,我不能实现路由(使用路由插入另一个模板)。Node JS - 使用路由器添加更多页面(ejs模板)

我的代码:观点/ 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?

+0

你的问题是,如何使用ejs渲染页面的特定部分,例如仅内容? – rmjoia

+0

@rmjoia是的,就像SPA .. – EverGreen

+0

据我所知。你不能。 ejs不支持布局,至少使用快递4.我一直在寻找... – rmjoia

回答

1

我想你期待像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-当地人其他模板。