Template’s settings: disallow_functions - forbid use functions in data $T [default: false, depend on: clone_data, clone_params] filter_data - replace chars: <, >, &, ’ and " in data $T to HTML entities [default: true, depend on: clone_data] filter_params - replace chars: <, >, &, ’ and " in parameters $P to HTML entities [default: false, depend on: clone_params] runnable_functions - automatically run function (from data) inside {} [default: false] clone_data - clone input data [default: true] clone_params - clone input parameters [default: true] f_cloneData - Function using to data cloning [default: TemplateUtils.cloneData] f_escapeString - Function using to escape strings [default: TemplateUtils.escapeHTML]
* jQuery.fn.processTemplateURL: type - ’GET’ or ’POST’, default: ’GET’ data - Data to be sent to the server. Default: undefined dataFilter - A function to be used to handle the raw response data of XMLHttpRequest. Default: undefined async - asynchronous AJAX, default: true cache - use cache, default: false timeout - Set a local timeout in ms for the request. on_success [callback] - Run after success on_error [callback] - Run on error on_complete [callback] - Run after success and error
jTemplates还支持#if、#for、#cycle、#foreach、#include、#param标签,帮助你处理实现复杂的业务情形。 #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}
#for 语法: {#for |VAR| = |CODE| to |CODE| [step=|CODE|]}..{#else}..{#/for} 或 {#for |variable| = |start| to |end| [step=|stepBy|]}..{#else}..{#/for} #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...}未能执行的时的输出内容。
#foreach 语法: {#foreach |VAR| as |NAME| [begin=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for} #foreach 示例: 默认:{#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} #foreach 内定环境变量: $index - index of element in table $iteration - id of iteration (next number begin from 0) $first - is first iteration? $last - is last iteration? $total - total number of iterations $key - key in object (name of element) (0.6.0+) $typeof - type of element (0.6.0+) #foreach 示例所需要的数据: var data = { name: "User list", list_id: 4, table: [ {id: 1, name: "Anne", age: 22, mail: "anne@domain.com"}, {id: 2, name: "Amelie", age: 24, mail: "amelie@domain.com"}, {id: 3, name: "Polly", age: 18, mail: "polly@domain.com"}, {id: 4, name: "Alice", age: 26, mail: "alice@domain.com"}, {id: 5, name: "Martha", age: 25, mail: "martha@domain.com"} ] }; (0.7.0+)版以后新增的功能,支持待循环集合用函数代替: {#foreach |FUNC| as |NAME| [begin=|CODE|] [end=|CODE|] [count=|CODE|] [step=|CODE|]}..{#else}..{#/for} 例: f = function(step) { if(step > 100) return null; // stop if loop is too long return "Step " + step; };
$("#result").setTemplate("{#foreach f as funcValue begin=10 end=20} {$T.funcValue}<br/> {#/for}"); $("#result").processTemplate(); #foreach在每次循环时请求的就是f函数,然后传递参数给f使用,并返回结果给funcValue变量