How to Use the EJS template in node. js

Source: Internet
Author: User
Tags classic asp
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:

  1. Cache Compiled functions are cached, requires filename

  2. Key name cached by filename

  3. Scope function execution scope

  4. Debug Output generated function body

  5. CompileDebug When false no debug instrumentation is compiled

  6. 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 %>.
  • <% } %>
      <% users.map(user) %>

    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.

         
    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.