This example describes the use of JavaScript text templates. Share to everyone for your reference. Specifically as follows:
This is my reference prism.js algorithm to write a small function, nothing to say, as a program ape to see examples should understand seconds.
String template Engine class:
/*class*/stringtemplate = function (/
* Optional {patt:regexp, clpatt:regexp}*/pattern) {
if (!!) Pattern) {
This.patt = Pattern.patt;
This.clpatt = Pattern.clpatt;
} else {
This.patt =/%\{\s*[\w\-]+\s*\}/g;
This.clpatt =/(^%\{\s*) | ( \s*\}$)/g;
}
This.format = function (val, map) {
var ls = [];
var res;
var prevend = 0;
while (res = This.patt.exec (val))!= null) {
var va = res[0];
var start = Val.substr (Prevend, res.index-prevend);
Prevend = Res.index + va.length;
Ls.push (start);
var vac = va.replace (This.clpatt, "");
Ls.push (Map[vac]);
Ls.push (Val.substr (Prevend, val.length));
Return Ls.join ("");
}
}
How to use:
var str = new Stringtemplate (). Format ("Hello%{username}, Welcome to login again%{systemname}", {userName: "Xiaoming", SystemName: "jb51"});
str= "Hello Xiaoming, welcome again landing jb51";
I hope this article will help you with your JavaScript programming.