velocityjs
Advanced tools
Comparing version 0.4.5 to 0.4.6
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"keywords": [ | ||
@@ -27,3 +27,6 @@ "velocity template" | ||
"test" : "make test" | ||
}, | ||
"spm": { | ||
"main": "index.js" | ||
} | ||
} |
125
README.md
@@ -35,53 +35,7 @@ Velocity - Template Engine | ||
不过,velocity模板本身适用于后端,用于前端不是很合适。 | ||
点击[此处](http://git.shepherdwind.com/velocity.js/try/index.html)可以体验web | ||
端velocity语法解析过程,注:使用ACE作为代码编辑器,仅支持高级浏览器访问。 | ||
执行`cake`命令进行打包velocity.js浏览器端脚本: | ||
虽然语法解释器可以在浏览器端执行,但是,不推荐那么用,后续不再打包浏览器版本。 | ||
```bash | ||
$ make parse | ||
``` | ||
需要cli下安装coffeejs,暂时打包是为kissy所使用的,velocity.js需要的一些常用的 | ||
ecma5功能,比如`foreach, some, isArray`等,在node环境下是自带的功能,而web端的兼 | ||
容是交给已有的类库解决。需要自行提供一组跨浏览器的api,比如kissy打包: | ||
```js | ||
//api map | ||
var utils = { | ||
forEach : S.each, | ||
some : S.some, | ||
mixin : S.mix, | ||
guid : S.guid, | ||
isArray : S.isArray, | ||
indexOf : S.indexOf, | ||
keys : S.keys, | ||
now : S.now | ||
}; | ||
``` | ||
Velocity语法具有很高的容错能力,类似于html结构的解析,同时语法规则复杂,所以语法 | ||
解释器执行性能可能比较慢,`velocity.js`把语法结构分析运算和语法执行两个过程独立出来, | ||
第一步,语法结构分析返回一个数组(语法树),描述velocity语法,语法执行使用数据和语 | ||
法树,计算模板最终结果。 | ||
执行build后,得到两个文件,分别是`build/velocity/`下的`index.js`和`parse.js`,两者 | ||
相互独立,`parse.js`语法分析过程可以放在本地完成,执行命令: | ||
把语法分析和模板拼接分开,为了方便在本地编译语法树,减少在web端js运算。本地编译 | ||
模板的思路,源自KISSY的[xtemplate](http://docs.kissyui.com/docs/html/api/component/xtemplate/)。 | ||
虽然语法解释器可以在浏览器端执行,但是,不推荐那么使用。 | ||
```bash | ||
#使用velocity命令行工具打包 | ||
veloctiy --build *.vm | ||
veloctiy -b *.vm | ||
``` | ||
源码中`test/web/`目录的js,一部分就是线下编译后的得到的,此处可[直接访问](http://shepherdwind.com/velocity/web/index.html)。 | ||
##Public API | ||
@@ -109,27 +63,5 @@ | ||
###On Broswer | ||
context中得函数,有一个固定的`eval`方法,可以用来运算vm语法字符串,比如webx对应的 | ||
`$control.setTemplate`的[实现](https://github.com/shepherdwind/velocity.js/blob/master/tests/compile.js#L532)。 | ||
1 . 使用线下打包方案: | ||
```js | ||
KISSY.use('velocity/index, web/directives', function(S, Velocity, asts){ | ||
var compile = new Velocity(asts); | ||
S.all('body').html(compile.render()); | ||
}); | ||
``` | ||
2 . 使用线上编译: | ||
```js | ||
KISSY.use('velocity/index, velocity/parse', function(S, Velocity, Parser){ | ||
var html = (S.all('#tpl').html()); | ||
var asts = Parser.parse(html); | ||
var compile = new Velocity(asts); | ||
console.log(compile.render()); | ||
}); | ||
``` | ||
两者的区别在于asts的获取,第一种方式,直接取asts,第二种,需要首先执行语法分析过 | ||
程。 | ||
##Syntax | ||
@@ -141,5 +73,5 @@ | ||
Directives支持`set`, `foreach`, `if|else|elseif`, `macro`, `parse`, `break`。不 | ||
支持有,`stop`, `evaluate`, `define`,感觉这些语法比较偏,用处不大,暂时没有实现。 | ||
其中`parse`,在web端,使用kissy的模块加载器加载,需要首先线下编译打包,[例子](http://shepherdwind.com/velocity/web/index.html)。 | ||
Directives支持`set`, `foreach`, `if|else|elseif`, `macro`, `break`。不 | ||
支持有,`stop`, `evaluate`, `define`, `parse`。不过可以通过context来实现,比如 | ||
`parse` [实现](https://github.com/shepherdwind/velocity.js/blob/master/tests/compile.js#L458)。 | ||
@@ -150,28 +82,5 @@ ###macro与parse | ||
外可以使用自定义的js函数替代在vm中定义。对于系统宏和自定义宏,不做区分,对于 | ||
`#parse`和`#include`的调用,可以使用自定义函数来执行。具体见[issue #3](https://github.com/shepherdwind/velocity.js/issues/3)。 | ||
`#parse`和`#include`的调用,可以使用自定义函数来执行,可以参考测试用例中self defined macro部分。 | ||
###foreach | ||
foreach在velocity中对对象的遍历,和js有区别,java中对象是一个map,需要通过方法 | ||
`keyset`来获取map中的key,foreach循环写法等同于js的for in循环,感觉有点怪异。在 | ||
一个foreach,有一个`$foreach`的对象可以使用,此变量作用域为当前循环范围。 | ||
``` | ||
#foreach($num in [1..5]) | ||
index => $foreach.index | ||
count => $foreach.count | ||
#if (!$foreach.hasNext) end #end | ||
#end | ||
结果: | ||
index => 0 | ||
count => 1 | ||
index => 1 | ||
count => 2 | ||
... | ||
index => 4 | ||
count => 5 | ||
end | ||
``` | ||
###string | ||
@@ -212,18 +121,10 @@ | ||
##Helper | ||
##Questions | ||
Helper提供一些额外的功能,主要用于解决vm数据模拟问题。 | ||
提问有几种方式 | ||
- `structure` 获取vm中所有变量的结构: `$foo.bar` => `foo: {bar: 'string'}` | ||
- `backstep` vm逆推,根据velocity文件和解析后的结果,计算数据结构和内容 | ||
- `jsonify` 把vm转换为json结构,去除其中的html标签,比如: | ||
1. 新建[issue](https://github.com/shepherdwind/velocity.js/issues/new) | ||
2. 邮件到eward.song at gmail.com | ||
3. 阿里内部员工,可以通过hanwen.sah搜到我的旺旺 | ||
jsonify文档[issue #11](https://github.com/shepherdwind/velocity.js/issues/11) | ||
``` | ||
hello world $foo.name. | ||
=> | ||
{foo: { name: $foo.name }} | ||
``` | ||
##License | ||
@@ -233,3 +134,3 @@ | ||
Copyright (c) 2012-2013 Eward Song<eward.song@gmail.com> | ||
Copyright (c) 2012-2013 Eward Song<eward.song at gmail.com> | ||
@@ -236,0 +137,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of |
var utils = require('../utils'); | ||
var Helper = require('../helper/'); | ||
function Velocity(asts) { | ||
var Helper = require('../helper/index'); | ||
function Velocity(asts, config) { | ||
this.asts = asts; | ||
this.config = { | ||
// 自动输出为经过html encode输出 | ||
escape: true, | ||
// 不需要转义的白名单 | ||
unescape: {} | ||
}; | ||
utils.mixin(config, config); | ||
this.init(); | ||
@@ -6,0 +13,0 @@ } |
@@ -62,3 +62,3 @@ module.exports = function(Velocity, utils){ | ||
utils.forEach(key, function(key){ | ||
this.unescape[key] = true | ||
this.config.unescape[key] = true | ||
}, this) | ||
@@ -77,4 +77,5 @@ | ||
if (ast.prue) { | ||
if (ast.id in this.unescape) ast.prue = false | ||
if (ast.id in this.config.unescape) ast.prue = false | ||
} | ||
var escape = this.config.escape; | ||
@@ -90,3 +91,3 @@ var isSilent = this.silence || ast.leader === "$!"; | ||
if (text in context) { | ||
return ast.prue ? convert(context[text]) : context[text]; | ||
return (ast.prue && escape) ? convert(context[text]) : context[text]; | ||
} | ||
@@ -106,3 +107,3 @@ | ||
//第三个参数,返回后面的参数ast | ||
ret = this.getAttributes(property, ret, ast.path.slice(i + 1), ast); | ||
ret = this.getAttributes(property, ret); | ||
@@ -116,3 +117,3 @@ }, this); | ||
ret = ast.prue ? convert(ret) : ret | ||
ret = (ast.prue && escape) ? convert(ret) : ret | ||
@@ -151,8 +152,5 @@ return ret; | ||
* 第二次是$a.b返回值 | ||
* @param {array} others 后面的属性,比如$a.b.c,求$a.b时,其余的是[c]所对 | ||
* 应的描述 | ||
* @param {object} total 整个ast描述 | ||
* @private | ||
*/ | ||
getAttributes: function(property, baseRef, others, total){ | ||
getAttributes: function(property, baseRef){ | ||
/** | ||
@@ -165,3 +163,3 @@ * type对应着velocity.yy中的attribute,三种类型: method, index, property | ||
if (type === 'method'){ | ||
ret = this.getPropMethod(property, baseRef, others, total); | ||
ret = this.getPropMethod(property, baseRef); | ||
} else if (type === 'property') { | ||
@@ -196,3 +194,3 @@ ret = baseRef[id]; | ||
*/ | ||
getPropMethod: function(property, baseRef, others, total){ | ||
getPropMethod: function(property, baseRef){ | ||
@@ -260,8 +258,2 @@ var id = property.id; | ||
baseRef.$sys = { | ||
others: others, | ||
vm: this, | ||
total: total | ||
}; | ||
baseRef.eval = function() { | ||
@@ -272,4 +264,2 @@ return that.eval.apply(that, arguments); | ||
delete baseRef.$sys; | ||
} else { | ||
@@ -276,0 +266,0 @@ ret = undefined; |
54
3892
443203
147