Use of ejs templating language

characteristic

  • <% %> for control flow

  • <%= %> output for escaping

  • <%- %> for unescaped output

  • -%> end tag for newline removal mode

  • Control flow with <%_ _%>whitespace removal patterns

  • Custom delimiters (for example, use '<? ?>' instead of '<% %>')

  • Include

  • Client support

  • Static caching of intermediary JavaScript

  • Static caching of templates

  • Compatible with  Express  view system

Example

<% if (user) { %>
  <h2><%= user.name %></h2>
<% } %>

usage

var template = ejs.compile(str, options);
template(data);
// => 渲染 HTML 字符串

ejs.render(str, data, options);
// => 渲染 HTML 字符串

You can also use shortcuts  ejs.render(dataAndOptions); , where you can pass anything through an object. In this case, you need to end up with a local variable that holds all the objects you need to pass.

Options

  • cache Compiled functions are cached and requirefilename

  • filename is cacheused as a cache key to contain

  • context the context in which the function executes

  • compileDebug If yes false, the tools for debugging will not be compiled

  • client Returns a standalone compiled function

  • delimiter character used to open or close angle brackets

  • debug Output the generated function body

  • _with Whether to use  with() {} structure. If  false so then local data will be stored in the  locals object.

  • rmWhitespace Removes all whitespace characters that can be safely removed, including leading and trailing whitespace characters. -%>It also enables a safer mode of newline truncation for all scriptlet tags . (It doesn't strip label wrapping within a line).

Label

  • <% 'Scriptlet' tag, for control flow, no output

  • <%= output value to template (with escaping)

  • <%- output unescaped value to template

  • <%# Comment labels, do not execute, and have no output

  • <%% output literal '<%'

  • %> normal end tag

  • -%> Trim-mode ('newline slurp') tag, removes subsequent newlines

Include

包含要么是绝对路径,或者如果不是的话,被视为相对于调用include的模板的路径(需要filename选项)。 例如,你在./views/users.ejs中包含./views/user/show.ejs,你应该使用<%- include('user/show') %>

你可能会用到原始输出标签(<%-)避免二次转义HTML输出。

<ul>
  <% users.forEach(function(user){ %>
    <%- include('user/show', {user: user}) %>
  <% }); %>
</ul>

包含的内容在运行时插入, 所以你可以在include调用中使用变量作为路径(例如<%- include(somePath) %>)。在你顶级数据对象中的变量都可以用于所有的包含,而局部变量需要传递进来。

注意:仍然支持包含预处理指令(<% include user/show %>)。

自定义分隔符

自定义分隔符可以以模板为单位应用,或者全局:

var ejs = require('ejs'),
    users = ['geddy', 'neil', 'alex'];

// Just one template
ejs.render('<?= users.join(" | "); ?>', {users: users}, {delimiter: '?'});
// => 'geddy | neil | alex'

// Or globally
ejs.delimiter = '$';
ejs.render('<$= users.join(" | "); $>', {users: users});
// => 'geddy | neil | alex'

缓存

EJS 自带了一个基本的运行时缓存,用于缓存渲染模板的中介JavaScript函数。使用 Node 的 lru-cache 库来添加LRU缓存十分简单:

var ejs = require('ejs')
  , LRU = require('lru-cache');
ejs.cache = LRU(100); // LRU cache with 100-item limit

如果你想清除ejs的缓存,调用ejs.clearCache。如果你需要以一个不同的限额来使用LRU,只需要将ejs.cache重新设置为一个LRU的新实例。

布局

EJS 不会特别地支持区块,但是可以采用包含头部和尾部的方法来实现局部,像这样:

<%- include('header') -%>
<h1>
  Title
</h1>
<p>
  My page
</p>
<%- include('footer') -%>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843132&siteId=291194637