Complete code:
Copy codeThe 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 codeThe 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 use
Copy 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!