Git Product home page Git Product logo

Comments (13)

shepherdwind avatar shepherdwind commented on July 20, 2024

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.

popomore avatar popomore commented on July 20, 2024

谢谢答复,我先尝试下

对于 foreach 那段不是很理解,直接输出到 数组中?

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024
#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.

shepherdwind avatar shepherdwind commented on July 20, 2024

我新建了一个issue #11 ,主要都放在那里面吧。

from velocity.js.

popomore avatar popomore commented on July 20, 2024

这个可以关了,已了解如何使用

from velocity.js.

popomore avatar popomore commented on July 20, 2024

详细问下关于 foreach 的使用,在使用这个 json 的时候是不是还要当作 vm 渲染一下?

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

整体过程是,首先编译成一个包含数据结构的vm,vm需要在后端的webx环境运行一遍,才能得到数据:如下图
比如,a.vm,jsonify得到文件a-json.vm,然后你需要把a.vm的底部增加一行代码

<!-- #parse('path/to/a-json.vm) -->

然后在后端daily环境下运行,可以在a.htm的请求里面的注释中获取到页面对应的json数据。

a-fs8

from velocity.js.

popomore avatar popomore commented on July 20, 2024

如果是函数的话是直接写么

{
  "bbb": "function(){return 'sth'}"
}

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

如果是函数,统一返回"function(){}",这一块还没想好怎么处理。后面会改的。

from velocity.js.

popomore avatar popomore commented on July 20, 2024

就是说现在输出的 json 还无法修改函数

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

嗯,我的想法是,函数和其他数据分开放,函数生成一个js文件,每个函数都关注于输入和输出关系的,输入和输出需要通过vm解析得到,目标生成的函数是下面这样的:

{
  "bbb": function(input){
     var output = [
       {arg: [1, 2], result: ["a"]} // ...
     ];
     return output[0]['result'];
   }
}

这一块还没有处理,感觉需要用到的情况不是很多。这个需要暂时自己处理。另一方面,函数的参数获取比较麻烦,有时候参数来自foreach,来自macro,这个运算过程还没搞明白如何弄。等jsonify其他功能都完善了再处理。

from velocity.js.

popomore avatar popomore commented on July 20, 2024
{
  "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.

shepherdwind avatar shepherdwind commented on July 20, 2024

如果是模拟的话,直接写json数组就行。这样就不需要通过后端的webx来运行了。

from velocity.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.