The examples in this article describe the JavaScript template engine usage. Share to everyone for your reference. Specifically as follows:
The template engine described here is very short and very well worth a visit.
Tmpl.js files are as follows:
Simple JavaScript Templating//John resig-http://ejohn.org/-MIT licensed (function () {var cache = {}; This.tmpl = function Tmpl (str, data) {//Figure out if we ' re getting a template, or if we need to//load the Templat
E-and Be sure to cache the result.
var fn =!/\w/.test (str)? CACHE[STR] = Cache[str] | | Tmpl (document.getElementById (str). InnerHTML)://Generate A reusable function that would serve as a template//
Generator (and which would be cached). New Function ("obj", "var p=[],print=function () {p.push.apply (p,arguments);};" +//introduce the data as local Var Iables using with () {} ' with (obj) {p.push (' +//Convert the template into pure JavaScript str. replace (/[ \r\t\n]/g, ""). Split ("<%"). Join ("\ T"). Replace ((^|%>) [^\t]*) '/g, ' $1\r '). Replace (/\t= (. *?)
%>/g, "', $, '"). Split ("T"). Join ("');") . Split ("%>"). Join ("P.push"). Split ("\ r"). Join ("\ \") +
"');} return P.join ('); "); Function ends//provide some basic currying to the user return data?
fn (data): FN;
};
})();
The
index.html file is as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
The
wants this article to help you with your JavaScript programming.