Comments (13)
helper现在在维护的只有jsonify这个了。在命令行下使用
velocity -j a.vm
velocity --jsonify a.vm > a.data.vm
或者
var Velocity = require('velocityjs');
var Jsonify = Velocity.Helper.Jsonify;
var asts = Parser.parse(fs.readFileSync(file).toString());
var makeup = new Jsonify(asts);
console.log(makeup.toVTL());
现在实现的功能主要是三种数据结构输出:
- 字符串,包括数字
- list数组
- map对象
对于复杂的结构,现在支持数据对象,比如:
#foreach($bd in $foo.tt)
$bd.a
$bd.b
$bd.c
#end
输出
{
"foo": {
"tt": [
#foreach($foo in $foo.tt) {
"a" : "$foo.a" ,
"b" : "$foo.b" ,
"c" : "$foo.c"
} #if($velocityCount < $foo.tt.size()) ,
#end
#end
]
}
}
对于foreach或者macro的调用,会找到全局变量中的变量,对于简单数据基本可以做到数据输出。
from velocity.js.
谢谢答复,我先尝试下
对于 foreach 那段不是很理解,直接输出到 数组中?
from velocity.js.
#foreach($bd in $foo.tt)
$bd.a
$bd.b
$bd.c
#end
这样一段vm,对应的数据结构是[{a: 1, b: 2}, {a:2, b: 1}]
,比普通的要复杂一些,而且,如果不是map,也不是list,可能直接是java对象,只能通过属性的方式去读取。
Jsonify的基本**是,根据vm,生成一个数据结构,并且把数据对于的vm变量输出,这样,把这个vm放到后端,让后端渲染,就可以等到数据了。
from velocity.js.
我新建了一个issue #11 ,主要都放在那里面吧。
from velocity.js.
这个可以关了,已了解如何使用
from velocity.js.
详细问下关于 foreach 的使用,在使用这个 json 的时候是不是还要当作 vm 渲染一下?
from velocity.js.
整体过程是,首先编译成一个包含数据结构的vm,vm需要在后端的webx环境运行一遍,才能得到数据:如下图
比如,a.vm,jsonify得到文件a-json.vm,然后你需要把a.vm的底部增加一行代码
<!-- #parse('path/to/a-json.vm) -->
然后在后端daily环境下运行,可以在a.htm的请求里面的注释中获取到页面对应的json数据。
from velocity.js.
如果是函数的话是直接写么
{
"bbb": "function(){return 'sth'}"
}
from velocity.js.
如果是函数,统一返回"function(){}",这一块还没想好怎么处理。后面会改的。
from velocity.js.
就是说现在输出的 json 还无法修改函数
from velocity.js.
嗯,我的想法是,函数和其他数据分开放,函数生成一个js文件,每个函数都关注于输入和输出关系的,输入和输出需要通过vm解析得到,目标生成的函数是下面这样的:
{
"bbb": function(input){
var output = [
{arg: [1, 2], result: ["a"]} // ...
];
return output[0]['result'];
}
}
这一块还没有处理,感觉需要用到的情况不是很多。这个需要暂时自己处理。另一方面,函数的参数获取比较麻烦,有时候参数来自foreach,来自macro,这个运算过程还没搞明白如何弄。等jsonify其他功能都完善了再处理。
from velocity.js.
{
"foo": {
"tt": [
#foreach($foo in $foo.tt) {
"a" : "$foo.a" ,
"b" : "$foo.b" ,
"c" : "$foo.c"
} #if($velocityCount < $foo.tt.size()) ,
#end
#end
]
}
}
还有现在要模拟数组是怎么写的,按上面解析出来的应该是空数组吧。
from velocity.js.
如果是模拟的话,直接写json数组就行。这样就不需要通过后端的webx来运行了。
from velocity.js.
Related Issues (20)
- Dashes in Identifiers HOT 1
- Support format quiet syntax $!{reference}
- String.replace() is not global HOT 2
- Map.remove() is not implemented HOT 2
- String pipe symbol not implemented HOT 1
- Support the $util.qr directive HOT 1
- IE or older browsers not supported!!! HOT 1
- Bug with #set inside #foreach: setting properties on loop variable from foreach doesn't work HOT 1
- TypeError: Cannot read property 'charCodeAt' of undefined HOT 1
- 如何实现webpack的热更新? HOT 2
- String matches does not work HOT 2
- 变量名称是null时报错 HOT 1
- Bug with rendering #foreach and $util.map.copyAndRemoveAllKeys HOT 13
- How to figure out which references in AST are able to be set in context HOT 2
- Support method with arguments HOT 2
- Parse String to Int HOT 2
- ESM module build? HOT 10
- #set with array fails for unknown reason HOT 1
- 无法解析vue中使用element-ui的$message语法 HOT 1
- Date functions not working HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from velocity.js.