ejs:找不到包含文件

时间:2019-09-15 16:04:29

标签: javascript node.js express ejs

我是一个做项目的学生。 我正在尝试从服务器获取书籍列表,并使用ejs逐一显示。

项目结构:

 |
 |-----routes
 |       |-----index.js
 |-----views
 |       |
 |       |----catalogue.ejs
 |       |----partials
 |       |      |----card.ejs
 |       |

index.js:

var express = require('express');
var router = express.Router();
router.get('/catalogue', function(req, res, next) {
  let books = [{ bookName: 'The Way of Kings',                  author: 'Brandon Sanderson',    price: 19.90,   id: '01' },
               { bookName: 'Words of Radiance',                 author: 'Brandon Sanderson',    price: 19.90,   id: '02' },
               { bookName: 'Oathbringer',                       author: 'Brandon Sanderson',    price: 19.90,   id: '03' },
               { bookName: '100 Paper Planes to fold & fly',    author: 'Usborne',              price: 13.90,   id: '04' },
               { bookName: 'Brain Games for Cleaver Kids',      author: 'Usborne',              price: 10.90,   id: '05' },
               { bookName: 'This is Going To Hurt',             author: 'Adam Kay',             price: 15.90,   id: '06' },
               { bookName: 'Normal People',                     author: 'Sally Ronney',         price: 10.90,   id: '07' },
               { bookName: 'Fahrenheit 451',                    author: 'Ray Bradbury',         price: 10.90,   id: '08' },
               { bookName: 'To be taught if fortunate',         author: 'Becky Chambers',       price: 9.90,    id: '09' },
               { bookName: 'The Better Sister',                 author: 'Alafer Burke',         price: 15.90,   id: '10' },
               { bookName: 'Crooked Kingdom',                   author: 'Leigh Bardugo',        price: 10.90,   id: '11' }
               ];

  res.render('catalogue', { books: books, page:'Catalogue', menuId:'catalogue'});
});

catalogue.ejs:

<!doctype html>
<html lang="en">
<head>
    <%- include partials/head %>
    <link rel="stylesheet" href="../../public/stylesheets/style.css">
</head>
<body>
<header>
    <%- include partials/header %>
</header>
<%- include partials/menu %>

<% books.forEach(function(book) { %>
    <%- include ('partials/card',{book:book}) %>
<% }); %>

<div class="container-fluid bg-3 text-center">
    <h3><%= page %></h3>
    <br>
</div>

<footer>
    <%- include partials/footer %>
</footer>

</body>
<%- include partials/script %>
</html>

card.ejs:

<main class="card container">
    <div class="card">
        <p class="bookName"><%= book.bookName %></p>
        <p class="author"><%= book.author %></p>
        <p class="price">Price: <%= book.price %>$</p>
        <p><button>Add to Cart</button></p>
    </div>
</main>

当我像这样运行它时,它仅显示以下错误:找不到包含文件。我从ejs文档中复制了循环模板,所以我不明白为什么它不起作用。

我尝试解决了这个问题,然后我意识到当目录中没有循环时,我只包含了局部代码/卡片就可以了。 但是,如果我尝试添加任何内容,即使是加括号,例如:include(partials / card)也会给我错误。是什么原因造成的?

感谢任何尝试提供帮助的人,我有一个截止日期。

1 个答案:

答案 0 :(得分:0)

您应该使用

<%- include partials/header %> 

不是

<% include partials/header %>

<%-应该用于包含文件

相关问题