Skip to content

Instantly share code, notes, and snippets.

@marsen
Last active August 29, 2015 13:57
Show Gist options
  • Save marsen/9883554 to your computer and use it in GitHub Desktop.
Save marsen/9883554 to your computer and use it in GitHub Desktop.
將template中所有的 {***} 變數 取代成 data[***]
////example
////http://goo.gl/FOMIgW
//template = '<li{is Selected}>{isChecked}<a href="/learn/{rootId}/topic/{courseId}">{indexStr} {courseName}</a></li>';
//var data = {
// isSelected: node.CourseId == Info.PageData.cId * 1 ? ' class="sel"' : "",
// isChecked: renderCheckedBox(node.CourseNodeType, node.Percentage, node.CourseId),
// indexStr: node.LevelIndex.join("-"),
// courseName: node.Name,
// rootId: Info.PageData.rId,
// courseId: node.CourseId
//};
////將template中所有的 {***} 變數 取代成 data[***]
function compile(template, data) {
return template.replace(/\{\s?([\w\s\.]*)\s?\}/g, function (str, key) {
key = $.trim(key);
var v = data[key];
return (typeof v !== 'undefined' && v !== null) ? v : '';
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment