如果我尝试编译如下所示的 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
最佳答案
它看起来像 EJS,也设置了一个名为 locals
的上下文,它被传递..
所以..
var html = ejs.render('Case <%= locals.case %>', data)
关于javascript - 在 ejs 中使用保留字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46575520/