模板语法
<% Javascript代码 %>:JavaScript代码<%= 变量名 %>:将HTML转义的值输出到模板中<%- 变量名 %>: 将未转义的值输出到模板中<%_ 变量名 _%>:去除所有空格<%# 注释内容 %>: 注释<% include 文件的路径 %>: 引入外部的文件
渲染方法
1 | let template = ejs.compile(str, options); |
Options配置项
cacheCompiled functions are cached, requiresfilenamefilenameUsed bycacheto key caches, and for includescontextFunction execution contextcompileDebugWhenfalseno debug instrumentation is compiledclientReturns standalone compiled functiondelimiterCharacter to use with angle brackets for open/closedebugOutput generated function body_withWhether or not to usewith() {}constructs. Iffalsethen the locals will be stored in thelocalsobject.localsNameName to use for the object storing local variables when not usingwithDefaults tolocalsrmWhitespaceRemove 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).escapeThe escaping function used with<%=construct. It is used in rendering and is.toString()ed in the generation of client functions. (By default escapes XML).outputFunctionNameSet to a string (e.g.,'echo'or'print') for a function to print output inside scriptlet tags.asyncWhentrue, EJS will use an async function for rendering. (Depends on async/await support in the JS runtime.