Nodejs + express + ejs

來源:互聯網
上載者:User

標籤: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)%>‘[{&#34;Name&#34;:&#34;davi&#34;,&#34;Time&#34;:1497591600000},{&#34;name&#34;:&#34;bill&#34;,&#34;Time&#34;:1497591600000},{&#34;name&#34;:&#34;can&#34;,&#34;Time&#34;:1497591600000},{&#34;name&#34;:&#34;can&#34;,&#34;Time&#34;: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.