0

我不能用 ejs 渲染布局,表达:

app.configure(function () {
    app.use(express.static(__dirname + '/public'));
    app.use(express.basicAuth('username', 'password'));
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
});

layout.ejs 位于 /views 文件夹中。我渲染 /views/home/index.ejs 但它不在布局中:

res.render('home/index');

布局.ejs:

<html>
  <head>
    <title></title>
      <script type="text/javascript" src="/content/scripts/jquery-1.6.2.min.js"></script>
      <script type="text/javascript" src="/content/scripts/jquery-ui-1.8.11.min.js"></script>
      <script type="text/javascript" src="/content/scripts/jquery.unobtrusive-ajax.min.js"></script>
      <script type="text/javascript" src="/content/scripts/jquery.validate.min.js"></script>
      <script type="text/javascript" src="/content/scripts/jquery.validate.unobtrusive.min.js"></script>
      <script type="text/javascript" src="/socket.io/socket.io.js"></script>
      <link rel="stylesheet" type="text/css" href="/content/styles/site.css" />
      <link rel="stylesheet" type="text/css" href="/content/styles/themes/base/jquery.ui.all.css"/>
  </head>
  <body>
      <%- body %>
  </body>
</html>
4

2 回答 2

12

我使用 ejs-locals 代替。它是 ejs-partials 的一个分支,但得到了积极维护。

npm install ejs-locals

https://github.com/RandomEtc/ejs-locals

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

还有一个布局,boilerplate.ejs:

<!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>
于 2012-10-21T18:58:27.730 回答
5

In express 3, you need to add partial and layout supports separately with express-partials

npm install express-partials
于 2012-10-21T16:17:16.933 回答