這裏是一個獨立的,定製的解決方案,我已經寫了是小得令人難以置信,並模仿原型的template system:
var templater = function(template, tokens, tokenIdentifier) {
tokenIdentifier = tokenIdentifier || "$";
// Decode HTML encoded template tokens %7B -> {, %7D -> }
template = template.replace(
new RegExp("\\" + tokenIdentifier + "%7B(\\w+)%7D", "gi"),
tokenIdentifier + "{$1}"
);
for (var i in tokens) {
if (tokens.hasOwnProperty(i)) {
template = template.replace(
new RegExp("\\"+tokenIdentifier+"\\{"+i+"\\}", "g"),
tokens[i]
);
}
}
return template;
};
用法:
templater("Hi, my name is ${name}", {name: "Bobo the Clown"});
// The token identifier defaults to $, but can be changed arbitrarily
templater("#{title} #{surname} #{verb} #{noun}", {title: "Dr.", surname: "Amazing", verb: "packed", noun: "sand"}, "#");
輸出:
Hi, my name is Bobo the Clown
Dr. Amazing packed sand
馬克,如果還活着,我專門構建我們的應用程序與純淨,如果您有任何問題,你可以在論壇發帖:http://groups.google.com/group/Pure-Unobtrusive-Rendering-Engine – Mic 2010-02-08 23:44:29