ejs是Embedded JavaScript的简写
var ejs = require('ejs'); var template = '<%= message %>'; var context = {message: 'Hello template!'}; console.log(ejs.render(template, context));//console.log(ejs.render(template, {locals: context}));
通过ejs.render(template, options)就可以将数据传递给模板,从而渲染展示出页面
var ejs = require('ejs'); var template = '<%= message %>'; var context = {message: "<script>alert('XSS attack!');</script>"}; console.log(ejs.render(template, context));
打印结果
<script>alert('XSS attack!');</script>
另:如果你确定传递的数据是可信的,则可以在模板标签中用<%-替代<%=,这样ejs就不会将特定的字符进行转义了
var ejs = require('ejs'); var template = '<%- message %>'; var context = { message: "<script>alert('Trusted JavaScript!');</script>"}; } console.log(ejs.render(template, context));
这样alert的弹窗效果就出来了
var ejs = require('ejs'); ejs.open = '{{' ejs.close = '}}' var template = '{{= message }}'; var context = {message: 'Hello template!'}; console.log(ejs.render(template, context));
var ejs = require('ejs'); var template = '<%=: movies | last %>'; var context = {'movies': [ 'Bambi', 'Babe: Pig in the City', 'Enter the Void' ]}; console.log(ejs.render(template, context)); //输出结果:Enter the Void
var ejs = require('ejs'); var template = '<%=: movies | get:0 %>'; var context = {'movies': [ 'Bambi', 'Babe: Pig in the City', 'Enter the Void' ]}; console.log(ejs.render(template, context)); //输出结果:Bambi
var ejs = require('ejs'); var template = '<%=: name | capitalize %>'; var context = {name: 'bob'}; console.log(ejs.render(template, context)); //输出结果:Bob
var ejs = require('ejs'); var template = '<%=: name | get:0 | upcase %>'; var context = {name: 'bob'}; console.log(ejs.render(template, context)); //输出结果:BOB
var ejs = require('ejs'); var template = '<%=: movies | size %>'; var context = {'movies': [ 'Bambi', 'Babe: Pig in the City', 'Enter the Void' ]}; console.log(ejs.render(template, context)); //输出结果:3
var ejs = require('ejs'); var template = '<%=: movies | length %>'; var context = {'movies': [ 'Bambi', 'Babe: Pig in the City', 'Enter the Void' ]}; console.log(ejs.render(template, context)); //输出结果:3
var ejs = require('ejs'); var template = '<%=: movies | get:1 | size %>'; var context = {'movies': [ 'Bambi', 'Babe: Pig in the City', 'Enter the Void' ]}; console.log(ejs.render(template, context)); //输出结果:21
var ejs = require('ejs'); var template = '<%=: movies | get:1| length %>'; var context = {'movies': [ 'Bambi', 'Babe: Pig in the City', 'Enter the Void' ]}; console.log(ejs.render(template, context)); //输出结果:21
var ejs = require('ejs'); var template = "<%=: movies | get:1 | get:'age' | plus:3 %>"; var context = {'movies': [ {name: 'Babe: Pig in the City',age: 19}, {name: 'Bambi', age: 21}, {name: 'Enter the Void', age: 13} ]}; console.log(ejs.render(template, context)); //输出结果:24
truncate:3(留3个字符) truncate_words:3(留3个单词)
var ejs = require('ejs'); var template = '<%=: title | truncate:3 %>'; var context = {title: 'The Hills are Alive With the Sound of Critters'}; console.log(ejs.render(template, context)); //输出结果:The
var ejs = require('ejs'); var template = '<%=: title | truncate_words:3 %>'; var context = {title: 'The Hills are Alive With the Sound of Critters'}; console.log(ejs.render(template, context)); //输出结果:The Hills are
var ejs = require('ejs'); var template = "<%=: weight | replace:'kilogram','kg' %>"; var context = {weight: '40 kilogram'}; console.log(ejs.render(template, context)); //输出结果:40 kg
var ejs = require('ejs'); var template = "<%=: weight | append:'kg' %>"; var context = {weight: '40 '}; console.log(ejs.render(template, context)); //输出结果:40 kg
var ejs = require('ejs'); var template = "<%=: weight | prepend:'$' %>"; var context = {weight: '40'}; console.log(ejs.render(template, context)); //输出结果:$40
var ejs = require('ejs'); var template = "<%=: movies | map:'age' | sort %>"; var context = {'movies': [ {name: 'Babe: Pig in the City',age: 19}, {name: 'Bambi', age: 21}, {name: 'Enter the Void', age: 13} ]}; console.log(ejs.render(template, context)); //输出结果:13,19,21
var ejs = require('ejs'); var template = "<%=: movies | map:'age' | sort | reverse %>"; var context = {'movies': [ {name: 'Babe: Pig in the City',age: 19}, {name: 'Bambi', age: 21}, {name: 'Enter the Void', age: 13} ]}; console.log(ejs.render(template, context)); //输出结果:21,19,13
var ejs = require('ejs'); var template = "<%=: movies | sort_by:'name' | first | get:'name' %>"; var context = {'movies': [ {name: 'Babe: Pig in the City'}, {name: 'Bambi'}, {name: 'Enter the Void'} ]}; console.log(ejs.render(template, context)); //输出结果:Babe: Pig in the City
var ejs = require('ejs'); var template = "<%=: movies | map:'age' %>"; var context = {'movies': [ {name: 'Babe: Pig in the City',age: 19}, {name: 'Bambi', age: 21}, {name: 'Enter the Void', age: 13} ]}; console.log(ejs.render(template, context)); //输出结果:19,21,13
var ejs = require('ejs'); var template = "<%=: movies | map:'age' | join:';' %>"; var context = {'movies': [ {name: 'Babe: Pig in the City',age: 19}, {name: 'Bambi', age: 21}, {name: 'Enter the Void', age: 13} ]}; console.log(ejs.render(template, context)); //输出结果:19;21;13
var ejs = require('ejs'); var template = '<%=: price * 1.14 | round:3 %>'; var context = {price: 21}; ejs.filters.round = function(number, decimalPlaces) { number = isNaN(number) ? 0 : number; decimalPlaces = !decimalPlaces ? 0 : decimalPlaces; return number.toFixed(decimalPlaces); }; console.log(ejs.render(template, context)); //输出结果:23.940
EJS支持可选的缓存模板功能,模板被解析后就缓存到内存中,这样渲染该模板速度会更快,因为这样省去了解析模板的过程
此功能一般只在production模式下使用,development模式下一般不使用.(产品模式下模板不会再有内容变动,而开发模式下,模板内容发生变化后,一般是希望立即看到效果,如果缓存起来却适得其反)
var cache = process.env.NODE_ENV === 'production'; var output = ejs.render( template, {students: students, cache: cache, filename: filename} );
该变量包含了所有通过app.set()所设置的变量,如果有了app.set('title', 'test'),在模板中我们可能直接通过settings.title来获取.
<html> <head> <title><%=settings.title%></title> </head> <body> <h1><%=settings.title%></h1> <p>Welcome to <%=settings.title%>.</p> </body>
如果index.ejs内容如下:
<%- include a %> hello world!
a.ejs内容为:
this is a.ejs
则通过index.ejs模板渲染出的内容为:
this is a.ejs hello world!