Jtemplate is a javascript template engine. Official Website: http://jtemplates.tpython.com/
Data preparation:
VaR DATA = {totalcount: 64, lists: [{ID: '000000', Title: 'news 11', createdate: '2017-08-08 '}, {ID: '20140901', Title: 'news 22', createdate: '1970-08-08 '}, {ID: '20160901', Title: 'news 33', createdate: '2017-08-08 '}, {ID: '20170901', Title: 'news 44', createdate: '2017-08-08'}, {ID: '20170901', title: 'news 55', createdate: '2017-08-08 '},]}
1. import warehouse receiving File
<script type="text/javascript" src="jquery.js"></script><script type="text/javascript" src="jquery-jtemplates.js"></script>
2. Compile a template
<Div id = "result"> </div> <Div id = "templatecontainer" style = "display: none; "> <Table> <tr> <TD> id </TD> <TD> title </TD> <TD> release date </TD> </tr> {# foreach $ T. table as row} <tr> <TD> {$ T. row. id} </TD> <TD >{$ T. row. title} </TD> <TD >{$ T. row. createdate }</TD> </TR >{#/for} </table> </div>
Syntax:
1,Braces {..}You can write any JavaScript code, such as {$ T. touppercase ()} 2,{$ T}: Indicates data. For example, in the preceding example, $ T. Table indicates the table object of data, and $ T. totalcount is 64. 3,{# Foreach}: Obtain data cyclically, as shown in the preceding figure: {# foreach $ T. Table as row }{$ T. Row. Title} {/}Extended Syntax:
{# If}
Example:
{#if $T=="true"} good {#else} fail {#/if}
{#if $T.list_id == 3} System list {#elseif $T.list_id == 4} Users List {#elseif $T.list_id == 5} Errors list {#/if}
{# Foreach}
{#foreach |VAR| as |NAME| [begin=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for}
Example:A. Output all data:
{#foreach $T.table as row} {$T.row.Title} {/for}
B. output from the second record:
{#foreach $T.table as row begin=1} {$T.row.Title} {/for}
C. Start from the second entry and take only 2
{#foreach $T.table as row begin=1 count=2} {$T.row.Title} {/for}
D. Use step
{#foreach $T.table as row step=2} {$T.row.Title} {/for}
E. Use else
{# Foreach $ T. Table as row step = 2} {$ T. Row. Title} {# else} No record {/}
{#}
Example:
{#for index = 1 to 10} {$T.index} {#/for}
{#for index = 1 to 10 step=3} {$T.index} {#/for}
3. rendering and displaying templates
<SCRIPT type = "text/JavaScript"> $ (document ). ready (function () {// set the template $ ("# result "). settemplateelement ("templatecontainer"); // process the template $ ("# result "). processtemplate (data) ;}); </SCRIPT>
You can set a template by using the following methods:A. settemplateelement:The parameter is an element ID in the page.B. settemplate:Parameters are specific template content, such as: $ ("# result "). settemplate ("template by {$ T. bold ()} version <em> {$ Q. version} </em>. ");C. settemplateurl:Use the external independent Template File URL as the parameter, for example, $ ("# result"). settemplateurl ("example_multitemplate1.tpl"); 4. Run the result: complete code
<HTML>