温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于jQuery的javascript前台模版引擎JTemplate

发布时间:2020-07-03 13:06:11 来源:网络 阅读:434 作者:宇宙小元帅 栏目:web开发

JTemplate是基于jQuery的开源的前端模版引擎,在Jtemplate模板中可以使用if判断、foreach循环、for循环等操作,使用Jtemplate模板优点在于ajax局部刷新界面时候不必要拼接html语句、可以通过ajax获取JSON格式的数据、在模版中允许使用javascript代码、允许你创建串接模版、允许你在模版中创建参数、即时刷新,自动从服务器端获取更新内容。
一、 jTemplate常用的标签有:
1、template 模版标签
2、if .. elseif .. else .. /if 条件语句
3、foreach .. else .. /for 循环
4、for .. else .. /for 循环
5、continue, break 继续或中断
二、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}
示例:
默认步长:{#for index = 1 to 10} {$T.index} {#/for}
正向步长:{#for index = 1 to 10 step=3} {$T.index} {#/for}
负向步长及空循环:{#for index = 1 to 10 step=-3} {$T.index} {#else} nothing {#/for}
也可以在循环中使用变量:{#for index = $T.start to $T.end step=$T.step} {$T.index} {#/for}
说明:{#else}是在{#for...}未能执行的时的输出内容。
(3)foreach语法
{#foreach |VAR| as |NAME| [begin=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for}示例:
默认:{#foreach $T.table as record} {$T.record.name} {#/for}
指定起始位置:{#foreach $T.table as record begin=1} {$T.record.name} {#/for}
指定起始和循环次数:{#foreach $T.table as record begin=1 count=2} {$T.record.name} {#/for}
指定步长:{#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>

绑定数据以及调用语句中关键的2句:
$('#result').setTemplateElement('template_1'); //设置模版
$('#result').processTemplate(data); //执行数据

原文参照博主个人网站:基于jQuery的javascript前台模版引擎JTemplate_IT技术小趣屋。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI