运行res.render时在EJS文件中出错



获取此错误:在编译ejs 时,C:\Users\yoniy\Desktop\YelpCap\views\campbgrounds\index.ejs中的参数列表后缺少(

这是index.ejs文件:

<!-- <%- include "../partials/header" %>  -->
<header class="jumbotron">
<div class="container">
<h1><span class="glyphicon glyphicon-tent"></span> Welcome To YelpCamp!</h1>
<p>View campgrounds from all over the world</p>
<p>
<a class="btn btn-primary btn-lg" href="/campgrounds/new">Add New Campground</a>
</p>
<p>
<form action="/campgrounds" method="GET" class="form-inline" id="campground-search">
<div class="form-group">
<input type="text" name="search" placeholder="Campground search..." class="form-control">
</div>
</form>
</p>
</div>
</header>

<div class="row text-center flex-wrap" id="campground-grid">
<%- campgrounds.forEach(function(campground){ %>
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="<%= campground.image %>">
<div class="caption">
<h4><%= campground.name %></h4>
</div>
<p>
<a href="/campgrounds/<%= campground._id %>" class="btn btn-primary">More Info</a>
</p>
</div>
</div>
<%- }); %>
</div>
<!-- <%- include "../partials/footer" %> -->

找不到我得到的语法错误。谢谢你的帮助!

您需要对模板的非输出部分使用流控制标记<%。在这种情况下,需要更改以下行:

<%- campgrounds.forEach(function(campground){ %>

应该是:

<% campgrounds.forEach(function(campground){ %>

这一行:

<%- }); %>

应为:

<% }); %>

更新其他错误

您包含其他文件的格式不正确:

<!-- <%- include "../partials/header" %>  -->

应为:

<%- include("../partials/header.ejs") %>

页脚相同:

<!-- <%- include "../partials/footer" %> -->

应为:

<%- include("../partials/footer.ejs") %>

虽然我无法验证您的路径是否正确,因为我不知道您的目录结构。

最新更新