1 | Velocity - Template Engine
|
2 | ==========================
|
3 | [![Build Status](https://travis-ci.org/shepherdwind/velocity.js.svg?branch=master)](https://travis-ci.org/shepherdwind/velocity.js)
|
4 | [![Coverage Status](https://img.shields.io/coveralls/shepherdwind/velocity.js/master.svg?style=flat)](https://coveralls.io/r/shepherdwind/velocity.js)
|
5 |
|
6 | [![NPM](https://nodei.co/npm/velocityjs.png?downloads=true)](https://nodei.co/npm/velocityjs/)
|
7 |
|
8 | velocity.js是[velocity](http://velocity.apache.org/)模板语法的javascript实现。
|
9 |
|
10 | ##Features
|
11 |
|
12 | - 支持客户端和服务器端使用
|
13 | - 语法分析和模板渲染分离
|
14 | - 基本完全支持velocity[语法](http://velocity.apache.org/engine/devel/user-guide.html)
|
15 | - [Vim Syntax](https://github.com/shepherdwind/vim-velocity)
|
16 |
|
17 | ##Install
|
18 |
|
19 | via npm:
|
20 |
|
21 | ```bash
|
22 | $ npm install velocityjs
|
23 | ```
|
24 |
|
25 | ##Broswer
|
26 |
|
27 | 兼容支持es5的浏览器,可以通过测试来验证[test case](http://git.shepherdwind.com/velocity.js/runner/tests.html)。
|
28 |
|
29 | 对于低端浏览器需要实现以下方法
|
30 |
|
31 | 1. Array.prototype的map, forEach, some, filter, every, indexOf
|
32 | 2. Date.now
|
33 | 3. Object.keys
|
34 |
|
35 | ##Examples
|
36 |
|
37 | 在tests目录下有大量的例子,node和浏览器下使用是一致的,另外,examples目录下有一个
|
38 | 最简单的例子。
|
39 |
|
40 | ##Public API
|
41 |
|
42 | 文件组织通过CommonJS方式,对于浏览器,通过spm可以打包为cmd模块。
|
43 |
|
44 | ```js
|
45 | var Velocity = require('velocityjs');
|
46 |
|
47 | //1. 直接解析
|
48 | Velocity.render('string of velocity', context, macros);
|
49 |
|
50 | //2. 使用parse和Compile
|
51 | var Compile = Velocity.Compile;
|
52 |
|
53 | var asts = Velocity.parse('string of velocity');
|
54 | (new Compile(asts)).render(context, macros);
|
55 | ```
|
56 |
|
57 | ####context
|
58 |
|
59 | `context`是一个对象,可以为空,执行中`$foo.bar`,访问路径是`context.foo.bar`,
|
60 | `context`的属性可以是函数,和vm中定义保持一致。
|
61 |
|
62 | context中得函数,有一个固定的`eval`方法,可以用来运算vm语法字符串,比如webx对应的
|
63 | `$control.setTemplate`的[实现](https://github.com/shepherdwind/velocity.js/blob/master/tests/compile.js#L532)。
|
64 |
|
65 | ##Syntax
|
66 |
|
67 | 具体语法请访问官网文档:[velocity user guide](http://velocity.apache.org/engine/devel/user-guide.html)。
|
68 |
|
69 | ###Directives
|
70 |
|
71 | Directives支持`set`, `foreach`, `if|else|elseif`, `macro`, `break`。不
|
72 | 支持有,`stop`, `evaluate`, `define`, `parse`。不过可以通过context来实现,比如
|
73 | `parse` [实现](https://github.com/shepherdwind/velocity.js/blob/master/tests/compile.js#L458)。
|
74 |
|
75 | ###macro与parse
|
76 |
|
77 | 宏分为系统的宏,比如`parse, include`,和用户自定义宏,通过`#macro`在vm中定义,此
|
78 | 外可以使用自定义的js函数替代在vm中定义。对于系统宏和自定义宏,不做区分,对于
|
79 | `#parse`和`#include`的调用,可以使用自定义函数来执行,可以参考测试用例中self defined macro部分。
|
80 |
|
81 | ##Questions
|
82 |
|
83 | 提问有几种方式
|
84 |
|
85 | 1. 新建[issue](https://github.com/shepherdwind/velocity.js/issues/new)
|
86 | 2. 邮件到eward.song at gmail.com
|
87 | 3. 阿里内部员工,可以通过hanwen.sah搜到我的旺旺
|
88 |
|
89 | ## 其他
|
90 |
|
91 | 推荐一下沉鱼写的[velocity](https://github.com/fool2fish/velocity)。
|
92 |
|
93 | ##License
|
94 |
|
95 | (The MIT License)
|