最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js: SyntaxError: Unexpected token : while compiling ejs - Stack Overflow

programmeradmin0浏览0评论

I am trying to apply filter to EJS using Node.JS and I am getting the below error. I want the message content to be displayed in uppercase.

D:\Apps\Templating\ejs>node server.js
D:\Apps\node_modules\ejs\lib\ejs.js:470
      throw e;
            ^
SyntaxError: Unexpected token : while piling ejs
    at Function (native)
    at Object.Templatepile (D:\Apps\node_m
odules\ejs\lib\ejs.js:460:12)
    at Objectpile (D:\Apps\node_modules\ej
s\lib\ejs.js:288:16)
    at handleCache (D:\Apps\node_modules\ejs\l
ib\ejs.js:147:16)
    at Object.exports.render (D:\Apps\node_mod
ules\ejs\lib\ejs.js:315:10)
    at Object.<anonymous> (D:\Apps\Templating\
ejs\server.js:3:17)
    at Module._pile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

This is my code- server.js:

var ejs = require('ejs');
var template = "<%=: message | upcase %>";
console.log(ejs.render(template,{message : 'hello ejs with filter'}));

I have installed ejs using the package manager and it is working for normal scripts with out filters(:). EJS version downloaded is the latest- 2.3.3 and node version is 0.12.4.

npm install ejs

Any help will be appreciated. Thanks in advance.

I am trying to apply filter to EJS using Node.JS and I am getting the below error. I want the message content to be displayed in uppercase.

D:\Apps\Templating\ejs>node server.js
D:\Apps\node_modules\ejs\lib\ejs.js:470
      throw e;
            ^
SyntaxError: Unexpected token : while piling ejs
    at Function (native)
    at Object.Template.pile (D:\Apps\node_m
odules\ejs\lib\ejs.js:460:12)
    at Object.pile (D:\Apps\node_modules\ej
s\lib\ejs.js:288:16)
    at handleCache (D:\Apps\node_modules\ejs\l
ib\ejs.js:147:16)
    at Object.exports.render (D:\Apps\node_mod
ules\ejs\lib\ejs.js:315:10)
    at Object.<anonymous> (D:\Apps\Templating\
ejs\server.js:3:17)
    at Module._pile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

This is my code- server.js:

var ejs = require('ejs');
var template = "<%=: message | upcase %>";
console.log(ejs.render(template,{message : 'hello ejs with filter'}));

I have installed ejs using the package manager and it is working for normal scripts with out filters(:). EJS version downloaded is the latest- 2.3.3 and node version is 0.12.4.

npm install ejs

Any help will be appreciated. Thanks in advance.

Share Improve this question asked Jul 16, 2015 at 7:56 hermes101hermes101 1761 gold badge2 silver badges8 bronze badges 4
  • <%=: isn't a valid construct, and ejs also doesn't support filters (like that). – robertklep Commented Jul 16, 2015 at 8:28
  • ejs do support filters like this. Please check this. github./tj/ejs – hermes101 Commented Jul 16, 2015 at 9:26
  • When you install ejs you install this module. The page you're refering to states: "NOTE: Version 2 of EJS makes some breaking changes with this version (notably, removal of the filters feature)". – robertklep Commented Jul 16, 2015 at 9:29
  • Yes. I found out that just now. I am not sure of the reson for removal. But it was a very handy feature. Thanks. – hermes101 Commented Jul 16, 2015 at 9:33
Add a ment  | 

3 Answers 3

Reset to default 6

The current version of ejs (v2.3.3) doesn't support filters, which were removed from version 2. If you want to use filters, you need to install a 1.x version:

$ npm i [email protected]

I found out this now. EJS made some changes from version 2 onwards including removal of filter feature. https://github./tj/ejs

in ejs v2 you have to put the code without = or - if there is normal code or function but not an object if you have an object you have to start with = or - Complies with the Express view system Static caching of intermediate JavaScript Unbuffered code for conditionals etc <% code %> Escapes html by default with <%= code %> Unescaped buffering with <%- code %> Supports tag customization Filter support for designer-friendly templates Includes Client-side support Newline slurping with <% code -%> or <% -%> or <%= code -%> or <%- code -%> see more information here : https://github./tj/ejs

发布评论

评论列表(0)

  1. 暂无评论