Juicer 轻量级javascript模板引擎

juicer是一个javascript轻量级模板引擎。

使用方法

编译模板并根据数据立即渲染出结果  

1 juicer(tpl, data);

仅编译模板暂不渲染,返回一个可重用的编译后的函数

1  var compiled_tpl = juicer(tpl);

根据给定的数据对之前编译好的模板进行渲染

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 缓存失效从而降低性能.

语法

* ${变量}

- 使用${}输出变量,其中_ 为对数据源的引用(${_})。支持使用自定义函数。

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); //输出 '&lt;strong&gt;juicer&lt;/strong&gt;'
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" >
      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  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);