javascript輕量級模板引擎juicer使用指南
使用方法
編譯模板并根據(jù)數(shù)據(jù)立即渲染出結(jié)果
juicer(tpl, data);
僅編譯模板暫不渲染,返回一個可重用的編譯后的函數(shù)
var compiled_tpl = juicer(tpl);
根據(jù)給定的數(shù)據(jù)對之前編譯好的模板進行渲染
var complied_tpl = juicer(tpl); var html = complied_tpl.render(data);
注冊/注銷自定義函數(shù)(對象)
juicer.register(‘function_name', function); juicer.unregister(‘function_name');
默認參數(shù)配置
{
cache: true [false];
script: true [false];
error handling: true [false];
detection: true [false];
}
修改默認配置,逐條修改
juicer.set('cache', false);
修改默認配置,批量修改
juicer.set({
'script': false,
'cache': false
})
Juicer 默認會對編譯后的模板進行緩存,從而避免同一模板多次數(shù)據(jù)渲染時候重復編譯所耗的時間, 如無特殊需要,強烈不建議關閉默認參數(shù)中的 cache,這么做將會令 Juicer 緩存失效從而降低性能.
語法
* ${變量}
- 使用${}輸出變量,其中_ 為對數(shù)據(jù)源的引用(${_})。支持使用自定義函數(shù)。
${name}
${name|function}
${name|function, arg1, arg2}
var = links: [{href: 'http://juicer.name', alt: 'Juicer'},
{href: 'http://benben.cc', alt: 'Benben'},
{href: 'http://ued.taobao.com', alt: 'Taobao UED'}
]};
var tpl = [ '{@each links as item}',
'${item|links_build} <br />',
'{@/each}'].join('');
var links = function(data) {
return '<a href="' + data.href + '" alt="' + data.alt + '" />';
};
juicer.register('links_build', links); //注冊自定義函數(shù)
juicer(tpl, json);
* 轉(zhuǎn)義/避免轉(zhuǎn)義
- ${變量} 在輸出之前會對其內(nèi)容進行轉(zhuǎn)義,如果你不想輸出結(jié)果被轉(zhuǎn)義,可以使用 $${變量} 來避免這種情況。
var json = {
value: '<strong>juicer</strong>'
};
var escape_tpl='${value}';
var unescape_tpl='$${value}';
juicer(escape_tpl, json); //輸出 '<strong>juicer</strong>'
juicer(unescape_tpl, json); //輸出 '<strong>juicer</strong>'
*循環(huán)遍歷 {@each} ... {@/each}
- 遍歷數(shù)組,${index}當前索引
{@each list as item, index}
${item.prop}
${index} //當前索引
{@/each}
*判斷 {@if} ... {@else if} ... {@else} ... {@/if}
*注釋 {# 注釋內(nèi)容}
{# 這里是注釋內(nèi)容}
*輔助循環(huán) {@each i in range(m, n)}
{@each i in range(5, 10)}
${i}; //輸出 5;6;7;8;9;
{@/each}
*子模板嵌套 {@include tpl, data}
- 子模板嵌套除了可以引入在數(shù)據(jù)中指定的子模板外,也可以通過指定字符串`#id`使用寫在`script`標簽中的模板代碼.
- HTML代碼:
<script type="text/juicer" id="subTpl">
I'm sub content, ${name}
</script>
- Javascript 代碼:
var tpl = 'Hi, {@include "#subTpl", subData}, End.';
juicer(tpl, {
subData: {
name: 'juicer'
}
});
//輸出 Hi, I'm sub content, juicer, End.
//或者通過數(shù)據(jù)引入子模板,下述代碼也將會有相同的渲染結(jié)果:
var tpl = 'Hi, {@include subTpl, subData}, End.';
juicer(tpl, {
subTpl: "I'm sub content, ${name}",
subData: {
name: 'juicer'
}
});
一個完整的例子
HTML 代碼:
<script id="tpl" type="text/template">
<ul>
{@each list as it,index}
<li>${it.name} (index: ${index})</li>
{@/each}
{@each blah as it}
<li>
num: ${it.num} <br />
{@if it.num==3}
{@each it.inner as it2}
${it2.time} <br />
{@/each}
{@/if}
</li>
{@/each}
</ul>
</script>
Javascript 代碼:
var data = {
list: [
{name:' guokai', show: true},
{name:' benben', show: false},
{name:' dierbaby', show: true}
],
blah: [
{num: 1},
{num: 2},
{num: 3, inner:[
{'time': '15:00'},
{'time': '16:00'},
{'time': '17:00'},
{'time': '18:00'}
]},
{num: 4}
]
};
var tpl = document.getElementById('tpl').innerHTML;
var html = juicer(tpl, data);
相關文章
Svelte框架實現(xiàn)表格協(xié)同文檔的示例
本文主要介紹了Svelte框架實現(xiàn)表格協(xié)同文檔的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01

