js之replace实现简单模板替换引擎

eg:

  var app={};

  app.tempEngine= (function () {
  var pattern = /\{(\w*[:]*[=]*\w+)\}(?!})/g;
  return function (template, json) {
  return template.replace(pattern, function (match, key, value) {
  return json[key];
     });
   }
})();

 

var template='<div>{name}</div>\

<div>{ege}</div>\

',data={

  name:"dabingzi",

      ege:28

}

 

$(app.tempEngine(template, data));

...../

...../

<div>dabingzi</div>\

<div>28</div>

 

posted @ 2016-05-26 18:26  大饼哥哥  阅读(2223)  评论(0编辑  收藏  举报