ejs模板引擎

ejs是一个js的模板引擎,基本的思路就是后台js提供数据,前端通过<%%>标签解析出来:

比如我们建立一个ejs文件:




    
    
    
    Document



   
iphone<%=a%>
    <% for(var i = 0;i
  • <%=news[i]%>
  • <%} %>

  然后我们有一个ejs_test.js文件提供数据渲染:

var ejs = require("ejs");
var fs = require("fs");

fs.readFile("./ejs_test.ejs",function (err,data) {
    var template  = data.toString();
    var dictionary = {a:66,news:["java","javascript","c"]};
    var html = ejs.render(template,dictionary);//用dictionary数据源填充template
    console.log(html);
})

  这里我们在控制台输出渲染完成后的内容:

ejs模板引擎_第1张图片

 

转载于:https://www.cnblogs.com/yk123/p/7171705.html

你可能感兴趣的:(ejs模板引擎)