HTML模板框架源代码
var HtmlTemplate = (function () { function HtmlTemplate(htmlSource) { this.htmlSource = htmlSource; } HtmlTemplate.prototype.toHTML = function (params) { var reg = HtmlTemplate.REG_FILTER_PARAM; var m = this.htmlSource.replace(reg, function (d) { var p = d.replace(HtmlTemplate.REG_GET_PARAM, ""); return params[p] || d; }); return m; }; HtmlTemplate.REG_FILTER_PARAM = /{{\s*[\w_]+\s*}}/gm; HtmlTemplate.REG_GET_PARAM = /[\s{}]+/g; return HtmlTemplate; })();
HTML模板框架使用Demo
var templateSource = "" + "<div>{{mm}}" + " <ul>" + " <li>dd{{ xx }}</li>" + " </ul>" + "</div>"; var template = new HtmlTemplate(templateSource); template.toHTML({ mm: "1111111111111", xx: "2222222222222" })
Demo运行结果