带有 ejs 的意外标识符



最近我觉得我一直在与 Ejs 模板引擎作斗争。我得到了意想不到的标识符,我似乎无法弄清楚为什么会这样。

错误

SyntaxError: pathviewsmain.ejs:34

这是来自 main.ejs 的以下代码行

<%- include('partials/comments'); %>

然后错误消息继续如下,

Unexpected identifier in pathviewspartialscomments.ejs while compiling ejs

来自 main.ejs 的相关行

<div class="col-sm-4">
<%- include('partials/stats'); %>
<%- include('partials/popular'); %>
<%- include('partials/comments'); %>
</div>

部分/评论.ejs

<div class="panel-body">
<ul class="media-list">
<% for(int i = 0; i < sidebar.comments.length; i++){ %>
<li class="media">
<a class="pull-left" href="/images/<%- sidebar.comments[i].image.uniqueId %>">
<img class="media-object" width="45" height="45" src="/public/upload/ <%- sidebar.comments[i].image.filename  %>">
</a>
<div class="media-body"><%- sidebar.comments[i].comment %>
<br/>
<strong class="media-heading"> <%- sidebar.comments[i].name %></strong>
<small class="text-muted"> <%- timeago(sidebar.comments[i].timestamp) %></small>
</div>
</li>
<% } %>
</ul>
</div>

导致此错误的原因是什么,我该如何解决?

我认为问题出在 ejs 上,而不是发送到comments.ejs模板的Viewmodel。以防万一,我在下面列出了这些功能。

视图模型的外观

const ViewModel = {
image:{
uniqueId: 1,
title: 'Sample Image 1',
description: 'This is a sample.',
filename: 'sample1.jpg',
Views: 0,
likes: 0,
timestamp: Date.now()
},
comments: [
{
image_id: 1,
email: 'test@testing.com',
name: 'Test Tester',
gravatar: 'http://lorempixel.com/75/75/animals/1',
comment: 'This is a test comment...',
timestamp: Date.now()
}, {
image_id: 1,
email: 'test@testing.com',
name: 'Test Tester',
gravatar: 'http://lorempixel.com/75/75/animals/2',
comment: 'Another followup comment!',
timestamp: Date.now()
}
],
state:'Images',
};

图片.js

const sidebar = require('../helpers/sidebar');
const path = require('path');
module.exports = {
index(req, res) {
sidebar(ViewModel, (ViewModel) => {
res.render('main', ViewModel);
});
}
}

侧 栏

const Stats = require('./stats')
const Images = require('./images')
const Comments = require('./comments');
module.exports = (ViewModel, callback) => {
ViewModel.sidebar = {
stats: Stats(),
popular: Images.popular(),
comments: Comments.newest()
};
callback(ViewModel);
};

评论.js

module.exports = {
newest() {
let comments = [
{
image_id: 1,
email: 'test@testing.com',
name: 'Test Tester',
gravatar: 'http://lorempixel.com/75/75/animals/1',
comment: 'This is a test comment...',
timestamp: Date.now(),
image: {
uniqueId: 1,
title: 'Sample Image 1',
description: '',
filename: 'sample1.jpg',
Views: 0,
likes: 0,
timestamp: Date.now
}
}, 
{
image_id: 1,
email: 'test@testing.com',
name: 'Test Tester ',
gravatar: 'http: //lorempixel.com/75/75/animals/2',
comment: 'Another followup comment!',
timestamp: Date.now(),
image: {
uniqueId: 1,
title: 'Sample Image 1',
description: '',
filename: 'sample1.jpg',
Views: 0,
likes: 0,
timestamp: Date.now
}
}
];
return comments;
}
};

错误消息:

Unexpected identifier in partials/comments.ejs while compiling ejs

告诉我们错误在 comments.ejs 中,果然:

<% for(int i = 0; i < sidebar.comments.length; i++){ %>

具体来说,int应该是letvar

相关内容

最新更新