Last active
August 29, 2015 13:57
-
-
Save marsen/9883554 to your computer and use it in GitHub Desktop.
將template中所有的 {***} 變數 取代成 data[***]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////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