search

Home  >  Q&A  >  body text

javascript - How to pass data to ejs template in ajax request?

The front end uses gulp to compile the ejs template into html. When actually using it, how does the data returned by the ajax request from the background be passed to the ejs template?

Generally, the template transfers data by first obtaining the template and then filling in the value. The data returned by the ajax request is in *.ejsHow to transfer the data to the template?

          var data = [{
                       "name":"张三",
                       "info":[
                         "眼睛",
                         "耳朵",
                         "鼻子"
                       ]
                     }];

       //获取模版
       var myTemplate = Handlebars.compile($("#table-template").html());
        
       //将json对象用刚刚注册的Handlebars模版封装,得到最终的html,插入到基础table中。
       $('#dataList').html(myTemplate(data));
仅有的幸福仅有的幸福2779 days ago539

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-18 10:49:54

    You can consider using underscore

    reply
    0
  • Cancelreply