This article mainly introduces the Quick Start learning of EJS templates. I think it is a good little bit. I will share it with you and give you a reference. Let's take a look at this article with a brief introduction to the Quick Start learning of EJS templates. I think this article is quite good. I will share it with you and give you a reference. Let's take a look at it with xiaobian.
Node open-source templates have many options, but we recommend that you use EJS for old people like me. It is natural to use EJS with Classic ASP/PHP/JSP experience. That is to say, you can... %> arrange JavaScript code in the block, and use the most traditional method <% = output variable %> (In addition, <%-output variables do not escape the & and other symbols ). The command for installing EJS is as follows:
npm install ejs
JS call
There are two main methods called by JS:
ejs.compile(str, options); // => Function ejs.render(str, options); // => str
In fact, EJS can be used independently from Express, for example:
var ejs = require(''), str = require('fs').readFileSync(dirname + '/list.ejs', 'utf8'); var ret = ejs.render(str, { names: ['foo', 'bar', 'baz'] }); console.log(ret);
See ejs. render (). The first parameter is the template string. The template is as follows.
<% if (names.length) { %>
<% names.forEach(function(name){ %>
- '><%= name %>
<% }) %>
<% } %>
Names becomes a local variable.
Option Parameter
The second parameter is data, which is generally an object. This object can be regarded as an option, that is, data and selection are on the same object.
If you do not want to make a disk every time, you can cache the template and set options. filename. For example:
var ejs = require('../') , fs = require('fs') , path = dirname + '/functions.ejs' , str = fs.readFileSync(path, 'utf8'); var users = []; users.push({ name: 'Tobi', age: 2, species: 'ferret' }) users.push({ name: 'Loki', age: 2, species: 'ferret' }) users.push({ name: 'Jane', age: 6, species: 'ferret' }) var ret = ejs.render(str, { users: users, filename: path }); console.log(ret);
The related options are as follows:
Cache Compiled functions are cached, requires filename
Key name cached by filename
Scope function execution scope
Debug Output generated function body
CompileDebug When false no debug instrumentation is compiled
Client Returns standalone compiled function
Inculde command
In addition, if you want
<% users.forEach(function(user){ %> <% include user/show %> <% }) %>
Generally, to insert a public template, that is, to introduce a file, you must set the filename option to start the include feature. Otherwise, the include feature cannot be known to the directory.
Template:
Users <% function user(user) { %>
<%= user.name %> is a <%= user.age %> year old <%= user.species %>. <% } %>
EJS supports template compilation. After template compilation, no I/O operations are performed, which is very fast and local variables can be shared. In the following example, user/show ignores the ejs extension:
<% users.forEach(function(user){ %> <% include user/show %> <% }) %>
Custom CLOSE TOKEN
If you want to use a non-<%> identifier like {= title}, you can also customize it.
var ejs = require('ejs'); ejs.open = '{{'; ejs.close = '}}';
You can also format the output.
ejs.filters.last = function(obj) { return obj[obj.length - 1]; };
Call
<%=: users | last %>
EJS also supports the browser environment.