標籤:oca blog class time info for nbsp ret ports
特性
<% %> 用於控制流程
<%= %> 用於轉義的輸出 (會對資料字元進行轉義)
// 資料來源// app.jsvar tem={ title:"我是中間部分", info:[{Name:"davi", Time:1497591600000},{name:"bill", Time:1497591600000},{name:"can", Time:1497591600000}]};// index.ejs// ‘<%=JSON.stringify(info)%>‘[{"Name":"davi","Time":1497591600000},{"name":"bill","Time":1497591600000},{"name":"can","Time":1497591600000},{"name":"can","Time":1497592600000}]
<%- %> 用於非轉義的輸出 (資料原本是什麼就輸出什麼)
-%> 結束標籤用於換行移除模式
帶有 <%_ _%> 的控制流程使用空白字元移除模式
添加外部js
ejs模板中未能直接使用外掛js 方法, 但能夠通過nodejs中 app.js 引入,掛載到 全域變數locals中,ejs中直接調用,直接上代碼:
// common.jsvar Common = { timeToDate: function(ts) { var Y, M, D, h, m, s; var date = new Date(ts); Y = date.getFullYear() + ‘-‘; M = (date.getMonth() + 1 < 10 ? ‘0‘ + (date.getMonth() + 1) : date.getMonth() + 1) + ‘-‘; D = (date.getDate() < 10 ? ‘0‘ + (date.getDate()) : date.getDate()) + ‘ ‘; h = (date.getHours() < 10 ? ‘0‘ + (date.getHours()) : date.getHours()) + ‘:‘; m = (date.getMinutes() < 10 ? ‘0‘ + (date.getMinutes()) : date.getMinutes()) + ‘:‘; s = (date.getSeconds() < 10 ? ‘0‘ + (date.getSeconds()) : date.getSeconds()); return (Y + M + D + h + m + s); }};module.exports = Common;
// nodejs app.jsvar http=require("http");var express=require("express");var fs = require("fs");var bodyParser = require(‘body-parser‘);var Common = require("./publice/common");var app=express();var tem={ title:"我是中間部分", info:[{Name:"davi", Time:1497591600000},{name:"bill", Time:1497591600000},{name:"can", Time:1497591600000}]};//掛載靜態資源處理中介軟體//app.locals.Common = Common;app.use(function(req, res, next){ res.locals.Common = Common; next();});............................
// index.ejs<div> <%info.forEach(function (name) { %> <dl class="clear-fix"> <dd class="wd150"><%= Common.timeToDate(name.Time)%></dd> <dd class="wd120 alc">50天</dd> <dd class="wd120 alc">¥100</dd> </dl> <%})%></div>
Nodejs + express + ejs