简易模板

分享  收藏
1 / 43

代码如下:

var sHtml = `<table>
  <tr>
    <td>A</td><td>B</td>
  </tr>
{loop}
  <tr>
	<td>{=Test[i].Name=}</td><td>D</td>
  </tr>
{/loop}
</table>`;
var i, rTest = [{"Name":"张三"},{"Name":"李四"}];
var rMatch, sLoop, sResult;
if (rMatch = sHtml.match(/{loop}([\s\S]*){\/loop}/m)) {
  sResult = '';
  for (i = 0; i < rTest.length; i++) {
    sLoop = rMatch[1];
    sLoop = sLoop.replace(/{=Test\[i\]\.Name=}/g, rTest[i].Name);
    sResult += sLoop;
  }
  sHtml = sHtml.replace(/{loop}([\s\S]*){\/loop}/m, sResult);
}
document.write(sHtml);