iv. template engine <a href= "<%= url%>" ></a> data binding, It becomes a complete HTML string. Front of the template, we now want to learn is the background of the template. Backstage template, the famous two, the first is called Ejs, the second one is called Jade. Is the NPM third-party package. First say ejsembedded JavaScript templates background template engine <ul> for (var i = 0; i < news.length; I + +) {%> <li><%= news[i]%></li> <%}%> </ul>var dictionary = { a:6, News: ["1 class is too good", "high-paying employment", "ha ha ha haha"]};
14.js
/**/var ejs = require ("Ejs"); // Templates var string = "So happy, today I bought iphone<%= a%>s"; // Data var data = { 6}; // Data Binding var html = Ejs.render (string, data); // output console.log (HTML);
15.js
/** * Created by Danny on 2015/9/20 16:59.*/varEjs = require ("Ejs");varFS = require ("FS");varHTTP = require ("http");varServer = Http.createserver (function(req,res) {fs.readfile ("./views/index.ejs",function(err,data) {//Binding Templates varTemplate =data.tostring (); varDictionary ={A:6, News: [{"title": "Chen Wei I Love You", "Count": 10}, {"title": "Haha haha", "count": 20}, {"title": "Tease You", "Count": 30} ] }; varHTML =Ejs.render (template,dictionary); //ShowRes.writehead (200,{"Content-type": "Text/html;charset=utf8"}); Res.end (HTML); });}); Server.listen (3000);
Index.ejs
<!doctype html> for (var i = 0; i < news.length; i++) { if(News[i].count >) { %> <li><%= news[i].title%></li> <% } } %> </ Ul></body>Ejs template efficiency is not high, jade engine high efficiency, difficult
Node08---ejs template