2018-11-10 20:36
如果你不想弄 root 这个根 name 来取值,需要做个扩展指令来将 root 对象赋给一个变量,然后取值,例如这么来用:
#setRoot()
然后后面的用法,与前面的完全一样,同样有个 root 对象可以使用
##set(keyList = [ ])
#for ( key : list_index)
#(keyList.add(key), null)
#end
#for ( key : keyList)
#for (x : root.get(key))
#(x.name)
#end
#end
关键在于这个 #setRoot() 指令怎么实现,其实极其简单,大致代码如下:
public class SetRootDirective extends Directive {
public void exec(Env env, Scope scope, Writer writer) {
scope.set("root", scope.getRootData());
}
}
本质就一行代码解决问题: scope.set("root", scope.getRootData());
最后别忘了配置一下:
engine.addDirective("setRoot", SetRootDirective.class);
2018-11-10 20:31
这个你得有办法获取最外层的 hash 对象,最好是取个名为好,例如:
{root : {
"list_index":["a","b","c",...,"z"],
"a":[{},{},...{}],
"b":[{},{},...{}],
...
"z":[{},{},...{}]
}
}
然后可以这么来玩:
#set(keyList = [ ])
#for ( key : list_index)
#(keyList.add(key), null)
#end
#for ( key : keyList)
#for (x : root.get(key))
#(x.name)
#end
#end