Complete Code :
Copy 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
Copy code The Code is as follows: <SCRIPT id = "template" type = "text/html">
<Table Style = "width: 400px;">
<Thead>
<Tr>
<TH> name </Th>
<TH> age </Th>
<TH> date </Th>
<TH> type </Th>
</Tr>
</Thead>
<Tbody>
<#
VaR Xing = items. PM;
#>
<Tr>
<TD>
<# = Xing. Key #>
</TD>
<TD>
<# = Xing. key1 #>
</TD>
<TD>
<# = Xing. Key #>
</TD>
<TD>
<# = Items. PM1 #>
</TD>
</Tr>
<#
#>
</Tbody>
</Table>
<Br/>
<# = Items. PM. Length #> record
</SCRIPT>
Then useCopy codeThe Code is as follows: $ (function (){
VaR JSON = {"items": {"PM": {"key": "value", "key1": "value1"}, "PM1 ": "pmvalue "}};
VaR output = $ ('# template'). parsetemplate (JSON );
Certificate ('{cc'}.html (output );
})
That's easy!