juicer是一個javascript輕量級模板引擎。javascript
編譯模板並根據數據當即渲染出結果 html
1 juicer(tpl, data);
僅編譯模板暫不渲染,返回一個可重用的編譯後的函數java
1 var compiled_tpl = juicer(tpl);
根據給定的數據對以前編譯好的模板進行渲染json
1 var complied_tpl = juicer(tpl); 2 var html = complied_tpl.render(data);
註冊/註銷自定義函數(對象)數組
1 juicer.register(‘function_name’, function); 2 juicer.unregister(‘function_name’);
默認參數配置緩存
1 { 2 cache: true [false]; 3 script: true [false]; 4 error handling: true [false]; 5 detection: true [false]; 6 }
修改默認配置,逐條修改函數
1 juicer.set('cache', false);
修改默認配置,批量修改性能
1 juicer.set({ 2 'script': false, 3 'cache': false 4 })
Juicer 默認會對編譯後的模板進行緩存,從而避免同一模板屢次數據渲染時候重複編譯所耗的時間, 如無特殊須要,強烈不建議關閉默認參數中的 cache,這麼作將會令 Juicer 緩存失效從而下降性能.ui
* ${變量} spa
- 使用${}輸出變量,其中_ 爲對數據源的引用(${_})。支持使用自定義函數。
1 ${name} 2 ${name|function} 3 ${name|function, arg1, arg2}
1 var = links: [{href: 'http://juicer.name', alt: 'Juicer’}, 2 {href: 'http://benben.cc', alt: 'Benben’}, 3 {href: 'http://ued.taobao.com', alt: 'Taobao UED’} 4 ]}; 5 var tpl = [ '{@each links as item}’, 6 '${item|links_build} <br />’, 7 '{@/each}'].join('’); 8 var links = function(data) { 9 return '<a href="' + data.href + '" alt="' + data.alt + '" />’; 10 }; 11 juicer.register('links_build', links); //註冊自定義函數 12 juicer(tpl, json);
* 轉義/避免轉義
- ${變量} 在輸出以前會對其內容進行轉義,若是你不想輸出結果被轉義,能夠使用 $${變量} 來避免這種狀況。
1 var json = { 2 value: '<strong>juicer</strong>' 3 }; 4 5 var escape_tpl='${value}'; 6 var unescape_tpl='$${value}'; 7 8 juicer(escape_tpl, json); //輸出 '<strong>juicer</strong>' 9 juicer(unescape_tpl, json); //輸出 '<strong>juicer</strong>'
*循環遍歷 {@each} ... {@/each}
- 遍歷數組,${index}當前索引
1 {@each list as item, index} 2 ${item.prop} 3 ${index} //當前索引 4 {@/each}
*判斷 {@if} ... {@else if} ... {@else} ... {@/if}
1 {@each list as item,index} 2 {@if index===3} 3 the index is 3, the value is ${item.prop} 4 {@else if index === 4} 5 the index is 4, the value is ${item.prop} 6 {@else} 7 the index is not 3, the value is ${item.prop} 8 {@/if} 9 {@/each}
*註釋 {# 註釋內容}
1 {# 這裏是註釋內容}
*輔助循環 {@each i in range(m, n)}
1 {@each i in range(5, 10)} 2 ${i}; //輸出 5;6;7;8;9; 3 {@/each}
*子模板嵌套 {@include tpl, data}
- 子模板嵌套除了能夠引入在數據中指定的子模板外,也能夠經過指定字符串`#id`使用寫在`script`標籤中的模板代碼.
- HTML代碼:
<script type="text/juicer" id="subTpl"> I'm sub content, ${name} </script>
- Javascript 代碼:
1 var tpl = 'Hi, {@include "#subTpl", subData}, End.'; 2 3 juicer(tpl, { 4 subData: { 5 name: 'juicer' 6 } 7 }); 8 9 //輸出 Hi, I'm sub content, juicer, End. 10 //或者經過數據引入子模板,下述代碼也將會有相同的渲染結果: 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 });
HTML 代碼:
1 <script id="tpl" type="text/template"> 2 <ul> 3 {@each list as it,index} 4 <li>${it.name} (index: ${index})</li> 5 {@/each} 6 {@each blah as it} 7 <li> 8 num: ${it.num} <br /> 9 {@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 代碼:
1 var data = { 2 list: [ 3 {name:' guokai', show: true}, 4 {name:' benben', show: false}, 5 {name:' dierbaby', show: true} 6 ], 7 blah: [ 8 {num: 1}, 9 {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);