模板语法
<% Javascript代码 %>
:JavaScript
代码<%= 变量名 %>
:将HTML
转义的值输出到模板中<%- 变量名 %>
: 将未转义的值输出到模板中<%_ 变量名 _%>
:去除所有空格<%# 注释内容 %>
: 注释<% include 文件的路径 %>
: 引入外部的文件
渲染方法
1 | let template = ejs.compile(str, options); |
Options配置项
cache
Compiled functions are cached, requiresfilename
filename
Used bycache
to key caches, and for includescontext
Function execution contextcompileDebug
Whenfalse
no debug instrumentation is compiledclient
Returns standalone compiled functiondelimiter
Character to use with angle brackets for open/closedebug
Output generated function body_with
Whether or not to usewith() {}
constructs. Iffalse
then the locals will be stored in thelocals
object.localsName
Name to use for the object storing local variables when not usingwith
Defaults tolocals
rmWhitespace
Remove all safe-to-remove whitespace, including leading and trailing whitespace. It also enables a safer version of-%>
line slurping for all scriptlet tags (it does not strip new lines of tags in the middle of a line).escape
The escaping function used with<%=
construct. It is used in rendering and is.toString()
ed in the generation of client functions. (By default escapes XML).outputFunctionName
Set to a string (e.g.,'echo'
or'print'
) for a function to print output inside scriptlet tags.async
Whentrue
, EJS will use an async function for rendering. (Depends on async/await support in the JS runtime.