如果我尝试编译如下的EJS模板:
var data = { case: 'Something' };
var html = ejs.render('Case <%= case %>', data);
我收到错误:
ejs.js:550 Uncaught SyntaxError: Unexpected token case while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
at new Function (<anonymous>)
at Template.compile (ejs.js:550)
at Object.compile (ejs.js:359)
at handleCache (ejs.js:202)
at Object.exports.render (ejs.js:385)
at VM546 script.js:2
因为case
是保留字。有没有办法绕过它,因为我不能改变我喂给EJS的JSON?
修改:这是指向在线Plunker的链接 - https://plnkr.co/edit/WBpjom
答案 0 :(得分:1)
它看起来像EJS,也设置了一个名为locals
的上下文传递..
所以......
var html = ejs.render('Case <%= locals.case %>', data)