Usually, when using ajax, we cannot avoid dealing with the lightweight json data format. However, it is difficult to manually parse json and construct HTML. Now with this plug-in, you can use the template to parse json as Extjs. Complete code:
The Code is as follows:
$. Fn. parseTemplate = function (data)
{
Var str = (this).html ();
Var _ tmplCache = {}
Var err = "";
Try
{
Var func = _ tmplCache [str];
If (! Func)
{
Var strFunc =
"Var p = [], print = function () {p. push. apply (p, arguments) ;};" +
"With (obj) {p. push ('" +
Str. replace (/[\ r \ t \ n]/g ,"")
. Replace (/'(? = [^ #] * #>)/G, "\ t ")
. Split ("'"). join ("\\'")
. Split ("\ t"). join ("'")
. Replace (/<# = (. + ?) #>/G, "', $1 ,'")
. Split ("<#"). join ("');")
. Split ("#>"). join ("p. push ('")
+ "');} Return p. join ('');";
// Alert (strFunc );
Func = new Function ("obj", strFunc );
_ TmplCache [str] = func;
}
Return func (data );
} Catch (e) {err = e. message ;}
Return "<# ERROR:" + err. toString () + "#> ";
}
Usage:
Declare this template first
The Code is as follows: