Skip to content

Instantly share code, notes, and snippets.

@iBaozi
Last active December 7, 2015 02:16
Show Gist options
  • Save iBaozi/9e6ada1ac2695ad12302 to your computer and use it in GitHub Desktop.
Save iBaozi/9e6ada1ac2695ad12302 to your computer and use it in GitHub Desktop.
js javascript 模板
// 模板
var floorTpl = '<div class="others" data-id="{id}">' +
'<span class="nick">{nick}</span>' +
'<span class="date">{datelineStr}</span>' +
'<div class="ctt">' +
'{content}' +
'<a href="javascript:void(0)"> [回复]</a>' +
'</div>' +
'</div>';
function tplRender(tpl, dataObj) {
return tpl.replace(/\{(\w+)\}/g, function ($0, $1) {
return dataObj[$1];
});
}
var data = {
nick: '包子',
datelineStr: '3月2日',
content: '今天天气不错'
}
var html = tplRender(floorTpl, data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment