juicer官方介绍
https://github.com/PaulGuo/Juicer
http://juicer.name/
juicer.set({
'tag::operationOpen': '{@',
'tag::operationClose': '}',
'tag::interpolateOpen': '${',
'tag::interpolateClose': '}',
'tag::noneencodeOpen': '$${',
'tag::noneencodeClose': '}',
'tag::commentOpen': '{#',
'tag::commentClose': '}'
});
{@each list as item,index}
{@if index===3}
the index is 3, the value is ${item.prop}
{@else if index === 4}
the index is 4, the value is ${item.prop}
{@else}
the index is not 3, the value is ${item.prop}
{@/if}
{@/each}
var tpl = 'Hi, {@include subTpl, subData}, End.';
juicer(tpl, {
subTpl: "I'm sub content, ${name}",
subData: {
name: 'juicer'
}
});
测试
<script type="text/javascript" src="../../javascript/jquery.js"></script>
<script type="text/javascript" src="../../javascript/juicer-min.js"></script>
<script type="text/javascript" src="../../javascript/jquery.json-2.3.js"></script>
<script type="text/javascript">
function renderToHtml(data){
var tpl = document.getElementById('tp1').innerHTML;
var html = juicer(tpl, data);
//alert(html);
jQuery("#content").html(html);
}
window.οnlοad=function(){
var indexOf = function getValueByIndex(arr,index){
if(arr.length
学校人数:${total} 学校名称: ${xx.name}
{@each cs as it,index}
-----------------------------${cs, index | indexOf}
{@/each}
{@each xslb as it,index}
学号 ${it.no}
姓名${it.name}
年龄 ${it.age}
成绩
{@each it.cj as it2,index}
行号:${index}
名次:${it2.no}
姓名:${it2.name}
成绩:${it2.score}
算术运算:${+it2.score/100}
索引为字符串:${index+1}
索引为数字:${+index+1}
{@/each}
{@/each}
文件名: juicer-1.html
<html>
<script type="text/javascript" src="juicer-min.js"></script> <!-- 引入 juicer -->
<script id="tpl" type="text/template"> <!-- template 模板; 模板内容不会直接显示出来 -->
<div>
<p> HTML script is allowed! </p>
<p> ${name} </p> <!-- ${variableName} juicer调用变量的方式 -->
</div>
</script>
<div id='content'> Original HTML content! </div>
<script type="text/javascript">
window.onload = function(){
alert('breakpoint');
var data=new Object();
data.name="this content is loaded by juicer!" // 定义一个字符串变量
var tpl = document.getElementById('tpl').innerHTML; // 获取模板内容
var tplContent = juicer(tpl, data); // 使用juicer将参数渲染模板
var content = document.getElementById('content');
content.innerHTML=tplContent; // 将内容写到HTML元素展示
}
</script>
</html>
更多使用实例,请参考以下 juicer-2.html 代码:
文件名: juicer-2.html
<html>
<script type="text/javascript" src="juicer-min.js"></script> <!-- -->
<!-- template 模板; 模板内容不会直接显示出来 ;;;; 此处type值可以设置为 type="text/XXX" 默认的javascript会导致浏览器解析出错。 -->
<script id="tpl" type="text/template">
<div>
<p> HTML script is allowed! </p>
<p> ${name} </p> <!-- ${variableName} juicer调用变量的方式 -->
<div>
{# 注释内容 }
<!-- 遍历js数组,将值给指定函数处理 -->
<span style="color:red"> list: </span> <br/>
{@each list as value,index}
{@if value.name=='guokai'}
The value is ${value.name},the index is ${index} <br/>
{@else}
value : ${value.name} ||| index : ${index} <br/>
{@/if}
{@/each}
<span style="color:red"> rows: </span> <br/>
{@each rows as value}
{@if value==1}
The value is ${value} <br/>
{@else if value == 2}
This value ${value} had been decorated : ${ value | decorate } <br/> {# 将变量传给函数处理 }
{@else}
The value is ${value} <br/>
{@/if}
{@/each}
</div>
</div>
</script>
<div>
<p> sub content! </p>
<!-- 新建一个模板,嵌套使用 -->
<script type="text/juicer" id="subTpl">
I'm 1th sub content, ${name}
</script>
</div>
<div id='content'> Original HTML content! </div>
<script type="text/javascript">
window.onload = function(){
alert('breakpoint');
//参数
var data=new Object();
data = {
list: [
{name:' guokai', show: true},
{name:' benben', show: false},
{name:' dierbaby', show: true}
],
name:"this content is loaded by juicer!", // 定义一个字符串变量
rows: [1,2,3]
}
var tpl = document.getElementById('tpl').innerHTML; // 获取模板内容
var tplContent = juicer(tpl); // 模板化tpl
var tplContent = tplContent.render(data);
var content = document.getElementById('content');
// 嵌套一个 subTpl 模板。(第 1 种方式)
var subtpl = '<br/><br/> Hi, {@include "#subTpl", subData}, End.<br/>';
tplContent += juicer(subtpl, {
subData: {
name: 'subJuicer'
}
});
// 嵌套一个 subTpl 模板。(第 2 种方式)
var tpl = 'Hi, {@include subTpl, subData}, End. <br/>';
tplContent += juicer(tpl, {
subTpl: "I'm 2th sub content, ${name}",
subData: {
name: 'juicer'
}
});
content.innerHTML=tplContent; // 将内容写到HTML元素展示
}
</script>
<script type="text/javascript">
// 自定义函数注册给juicer
function decorateString(value){
var newValue = value+10;
return newValue ;
}
juicer.register('decorate', decorateString);
</script>
</html>
</script>