EJS:无法加载资源:服务器使用循环

时间:2018-02-22 17:49:48

标签: node.js express ejs

我正在尝试循环浏览我的投资组合部分,同时使用ejs输入它。但它丢了500个错误。我创建了循环来减少代码。其他页面工作正常,但无法找到标题或路径丢失。在我添加循环之前它已经工作了。

const express = require('express');
const router = express.Router();

/* GET portfolio page. */
router.get('/portfolio', (req, res, next) => {
  let recentProjects = [{
      title: 'ASL Logo',
      paragraph: 'Re-branding of ASL Water Solutions'
    },
    {
      title: 'Siva Creative Business Card',
      paragraph: 'Re-branding of Siva Creative'
    },
    {
      title: 'Encore Renovations',
      paragraph: 'Website design for Encore'
    }
  ]
  res.render('portfolio', {
    projects: recentProjects
  });
});

module.exports = router;
<% include partials/header %>

  <section class="content-section" id="portfolio">
    <div class="container">
      <div class="content-section-heading text-center">
        <h3 class="text-secondary mb-0">
          <%= title %>
        </h3>
        <h2 class="mb-5">Recent Projects</h2>
      </div>
      <div class="row no-gutters">
        <% for(let i = 0; i < projects.length; i++) { %>
          <div class="col-lg-6">
            <a class="portfolio-item">
              <span class="caption">
                  <span class="caption-content">  
                    <h2><%= projects[i].title %></h2>
                    <p class="mb-0"><%= projects[i].paragraph %></p>
                  </span>
              </span>
              <img class="img-fluid" src="img/portfolio-1.jpg" alt="">
            </a>
          </div>
          <% } %>

      </div>
    </div>
  </section>
  <% include partials/footer %>

1 个答案:

答案 0 :(得分:1)

在您的视图中,未定义变量title,您应该像对recentProjects对象一样将其传递给视图:

res.render('portfolio', {
     projects: recentProjects,
     title: 'portfolio'
});