溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于jQuery的javascript前臺(tái)模版引擎JTemplate

發(fā)布時(shí)間:2020-07-03 13:06:11 來源:網(wǎng)絡(luò) 閱讀:450 作者:宇宙小元帥 欄目:web開發(fā)

JTemplate是基于jQuery的開源的前端模版引擎,在Jtemplate模板中可以使用if判斷、foreach循環(huán)、for循環(huán)等操作,使用Jtemplate模板優(yōu)點(diǎn)在于ajax局部刷新界面時(shí)候不必要拼接html語句、可以通過ajax獲取JSON格式的數(shù)據(jù)、在模版中允許使用javascript代碼、允許你創(chuàng)建串接模版、允許你在模版中創(chuàng)建參數(shù)、即時(shí)刷新,自動(dòng)從服務(wù)器端獲取更新內(nèi)容。
一、 jTemplate常用的標(biāo)簽有:
1、template 模版標(biāo)簽
2、if .. elseif .. else .. /if 條件語句
3、foreach .. else .. /for 循環(huán)
4、for .. else .. /for 循環(huán)
5、continue, break 繼續(xù)或中斷
二、jTemplates的語法:
(1)if語法
{#if |COND|}..{#elseif |COND|}..{#else}..{#/if}
#if 示例:
{#if $T.hello} hello world. {#/if}
{#if 2*8==16} good {#else} fail {#/if}
{#if $T.age>=18)} 成人了 {#else} 未成年 {#/if}
{#if $T.list_id == 3} System list {#elseif $T.list_id == 4} Users List {#elseif $T.list_id == 5} Errors list {#/if}
(2)for語法
{#for |VAR| = |CODE| to |CODE| [step=|CODE|]}..{#else}..{#/for}
示例:
默認(rèn)步長(zhǎng):{#for index = 1 to 10} {$T.index} {#/for}
正向步長(zhǎng):{#for index = 1 to 10 step=3} {$T.index} {#/for}
負(fù)向步長(zhǎng)及空循環(huán):{#for index = 1 to 10 step=-3} {$T.index} {#else} nothing {#/for}
也可以在循環(huán)中使用變量:{#for index = $T.start to $T.end step=$T.step} {$T.index} {#/for}
說明:{#else}是在{#for...}未能執(zhí)行的時(shí)的輸出內(nèi)容。
(3)foreach語法
{#foreach |VAR| as |NAME| [begin=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for}示例:
默認(rèn):{#foreach $T.table as record} {$T.record.name} {#/for}
指定起始位置:{#foreach $T.table as record begin=1} {$T.record.name} {#/for}
指定起始和循環(huán)次數(shù):{#foreach $T.table as record begin=1 count=2} {$T.record.name} {#/for}
指定步長(zhǎng):{#foreach $T.table as record step=2} {$T.record.name} {#/for}
三、Jtemplate模板使用方法如下:
<div id="result"></div>
<textarea id="template_1" style="display: none;">
<table>
<tr>
<th></th>
</tr>
{#foreach $T as record}
<tr>
<td>{$T.record.屬性名}</td>
</tr>
{#/for}
</table>
</textarea>

綁定數(shù)據(jù)以及調(diào)用語句中關(guān)鍵的2句:
$('#result').setTemplateElement('template_1'); //設(shè)置模版
$('#result').processTemplate(data); //執(zhí)行數(shù)據(jù)

原文參照博主個(gè)人網(wǎng)站:基于jQuery的javascript前臺(tái)模版引擎JTemplate_IT技術(shù)小趣屋。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI