带有 ejs 的意外标识符

Unexpected identifier with ejs

最近感觉自己一直在和Ejs模板引擎作斗争。我收到了意外的标识符,我似乎无法弄清楚为什么会这样。

错误

SyntaxError: path\views\main.ejs:34

这是来自main.ejs

的以下代码行
<%- include('partials/comments'); %>

然后报错信息继续如下,

Unexpected identifier in path\views\partials\comments.ejs while compiling ejs

来自main.ejs

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

partials/comments.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。为了以防万一,我在下面列出了这些功能。

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',
};

image.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);
};

comments.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