Created
November 26, 2016 07:43
-
-
Save skylying/71f855c6a6a3a1f6a68c77e97b9c29d1 to your computer and use it in GitHub Desktop.
Template syntax in javascript
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
var template = "<div><h1>{{title}}</h1></div> <div><h1>{{name}}</h1></div>" | |
var content = { | |
"title": 'Hello world' | |
"name": 'Jack' | |
} | |
function replacer(match, p1, offset, string) { | |
for (var key in content) { | |
if (p1.indexOf(key) != '-1') { | |
p1 = content[key] | |
}; | |
}; | |
// p1 is nondigits, p2 = digits, p3 = non-alphanumerics | |
return p1; | |
}; | |
replaced = template.replace(/(\{\{[\S]*\}\})/g, replacer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment