Juicer 是一個(gè)高效、輕量的前端 (Javascript) 模板引擎,使用 Juicer 可以是你的代碼實(shí)現(xiàn)數(shù)據(jù)和視圖模型的分離(MVC)。
使用方法
編譯模板并根據(jù)數(shù)據(jù)立即渲染出結(jié)果
view sourceprint?1 juicer(tpl, data);
僅編譯模板暫不渲染,返回一個(gè)可重用的編譯后的函數(shù)
view sourceprint?1 var compiled_tpl = juicer(tpl);
根據(jù)給定的數(shù)據(jù)對(duì)之前編譯好的模板進(jìn)行渲染
view sourceprint?1 var complied_tpl = juicer(tpl);
2 var html = complied_tpl.render(data);
注冊(cè)/注銷自定義函數(shù)(對(duì)象)
view sourceprint?1 juicer.register(‘function_name', function);
2 juicer.unregister(‘function_name');
默認(rèn)參數(shù)配置
view sourceprint?1 {
2 cache: true [false];
3 script: true [false];
4 error handling: true [false];
5 detection: true [false];
6 }
修改默認(rèn)配置,逐條修改
view sourceprint?1 juicer.set('cache', false);
修改默認(rèn)配置,批量修改
view sourceprint?1 juicer.set({
2 'script': false,
3 'cache': false
4 })
Juicer 默認(rèn)會(huì)對(duì)編譯后的模板進(jìn)行緩存,從而避免同一模板多次數(shù)據(jù)渲染時(shí)候重復(fù)編譯所耗的時(shí)間, 如無特殊需要,強(qiáng)烈不建議關(guān)閉默認(rèn)參數(shù)中的 cache,這么做將會(huì)令 Juicer 緩存失效從而降低性能.
語法
* ${變量}
- 使用${}輸出變量,其中_ 為對(duì)數(shù)據(jù)源的引用(${_})。支持使用自定義函數(shù)。
view sourceprint?1 ${name}
2 ${name|function}
3 ${name|function, arg1, arg2}
view sourceprint?01 var = links: [{href: 'http://juicer.name', alt: 'Juicer'},
02 {href: 'http://benben.cc', alt: 'Benben'},
03 {href: 'http://ued.taobao.com', alt: 'Taobao UED'}
04 ]};
05 var tpl = [ '{@each links as item}',
06 '${item|links_build} <br />',
07 '{@/each}'].join('');
08 var links = function(data) {
09 return '<a href="' + data.href + '" alt="' + data.alt + '" />';
10 };
11 juicer.register('links_build', links); //注冊(cè)自定義函數(shù)
12 juicer(tpl, json);
* 轉(zhuǎn)義/避免轉(zhuǎn)義
- ${變量} 在輸出之前會(huì)對(duì)其內(nèi)容進(jìn)行轉(zhuǎn)義,如果你不想輸出結(jié)果被轉(zhuǎn)義,可以使用 $${變量} 來避免這種情況。
view sourceprint?1 var json = {
2 value: '<strong>juicer</strong>'
3 };
4 var escape_tpl='${value}';
5 var unescape_tpl='$${value}';
6 juicer(escape_tpl, json); //輸出 '<strong>juicer</strong>'
7 juicer(unescape_tpl, json); //輸出 '<strong>juicer</strong>'
*循環(huán)遍歷 {@each} ... {@/each}
- 遍歷數(shù)組,${index}當(dāng)前索引
view sourceprint?1 {@each list as item, index}
2 ${item.prop}
3 ${index} //當(dāng)前索引
4 {@/each}
*判斷 {@if} ... {@else if} ... {@else} ... {@/if}
*注釋 {# 注釋內(nèi)容}
{# 這里是注釋內(nèi)容}
*輔助循環(huán) {@each i in range(m, n)}
view sourceprint?1 {@each i in range(5, 10)}
2 ${i}; //輸出 5;6;7;8;9;
3 {@/each}
*子模板嵌套 {@include tpl, data}
- 子模板嵌套除了可以引入在數(shù)據(jù)中指定的子模板外,也可以通過指定字符串`#id`使用寫在`script`標(biāo)簽中的模板代碼.
- HTML代碼:
view sourceprint?1 <script type="text/juicer" id="subTpl">
2 I'm sub content, ${name}
3 </script>
- Javascript 代碼:
view sourceprint?01 var tpl = 'Hi, {@include "#subTpl", subData}, End.';
02
03 juicer(tpl, {
04 subData: {
05 name: 'juicer'
06 }
07 });
08
09 //輸出 Hi, I'm sub content, juicer, End.
10 //或者通過數(shù)據(jù)引入子模板,下述代碼也將會(huì)有相同的渲染結(jié)果:
11
12 var tpl = 'Hi, {@include subTpl, subData}, End.';
13
14 juicer(tpl, {
15 subTpl: "I'm sub content, ${name}",
16 subData: {
17 name: 'juicer'
18 }
19 });
一個(gè)完整的例子
HTML 代碼:
view sourceprint?01 <script id="tpl" type="text/template">
02 <ul>
03 {@each list as it,index}
04 <li>${it.name} (index: ${index})</li>
05 {@/each}
06 {@each blah as it}
07 <li>
08 num: ${it.num} <br />
09 {@if it.num==3}
10 {@each it.inner as it2}
11 ${it2.time} <br />
12 {@/each}
13 {@/if}
14 </li>
15 {@/each}
16 </ul>
17 </script>
Javascript 代碼:
view sourceprint?01 var data = {
02 list: [
03 {name:' guokai', show: true},
04 {name:' benben', show: false},
05 {name:' dierbaby', show: true}
06 ],
07 blah: [
08 {num: 1},
09 {num: 2},
10 {num: 3, inner:[
11 {'time': '15:00'},
12 {'time': '16:00'},
13 {'time': '17:00'},
14 {'time': '18:00'}
15 ]},
16 {num: 4}
17 ]
18 };
19
20 var tpl = document.getElementById('tpl').innerHTML;
21 var html = juicer(tpl, data);
更多信息請(qǐng)查看IT技術(shù)專欄