node.js之EJS

github : https://github.com/visionmedia/ejs

ejs模板跟其餘模板差很少。git

與smarty模板的區別:github

  1. 開始標記(<% ;{ )和結束標記( %>;} )
  2. 模板函數名或功能稍有不一樣

安裝npm

npm install ejs

特色
api

模板邏輯代碼
    <% code %>
輸出變量值
    <%= code %>
不作轉義的輸出變量值
    <%- code %>

簡單示例app

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

ejs.compile(str, options);
// => Function

ejs.render(str, options);
// => str

<ul>
  <% users.forEach(function(user){ %>
    <% include user/show %>
  <% }) %>
</ul>

更改開始標記和結束標記ide

var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';

<h1>{{= title }}</h1>

函數列表函數

  • first
  • last
  • capitalize
  • downcase
  • upcase
  • sort
  • sort_by:'prop'
  • size
  • length
  • plus:n
  • minus:n
  • times:n
  • divided_by:n
  • join:'val'
  • truncate:n
  • truncate_words:n
  • replace:pattern,substitution
  • prepend:val
  • append:val
  • map:'prop'
  • reverse
  • get:'prop'

模板嵌套post

<% include head %>
<h1>Title</h1>
<p>My page</p>
<% include foot %>

轉載自:http://www.9958.pw/post/ejsui

相關文章
相關標籤/搜索