fool2fish / velocity

A node velocity template engine. Node 版 velocity 模板引擎。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

define变量的引用问题

gogoyqj opened this issue · comments

below is library.vm:

#macro(render $subchannel)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style tyle="text/css">
    ## 页面css
    $!q_css
</style>
</head>
<body>
    ## 页面html
    $!q_body
    ## 页面js
    $!q_js
</body>
</html>
#end
{code}

below is index.vm:

#define( $q_css )
body{background:green;}
#end

#define( $q_body )
hello word!
#end

#define( $q_js )
<script>
    var nimei = "nimei";
</script>
#end

#render('hotel')

在java里,render index.vm能输出$q_js,$q_css,$q_body,这样也是比较合理的

在engine-ref.js里

   Reference: function(node) {
    // call define
    if (common.isId(node)) {
      var name = node.object.name
      if (name in this.template.__define) {
        var def = this.template.__define[name]
        var result = this[def.type](def)
        if (result.stats !== STATS.SUCCESS) {
          // at where the #define is called
          result.stack.push(common.getRealLoc([this.template, node.pos]))
        }
        return result
      }
    }

是不是做成 return this.template._define[xxx] <- this.template.__parent.__define这样逐层查找比较合理,且和java更加吻合

这里不需要使用逐层查找,因为同名 define 的变量只记录最后一个。
但是没看出来你这段代码的问题是啥?最后没有输出么?library.vm 是全局 macro 定义还是在某处 include 的?

我和这个情况一样,library.vm是全局的,然后
#set($title = 'This is test')
#define( $css )
body{
background: #555;
}
#end
#render()
后没有输出

如1楼所说的,查找时候带上 this.template.__parent.__define。看代码和1楼居然是同一公司的。。。

@fool2fish index 引用了lib,然后调用lib里的macro,macro用到一个变量,在java里是如果lib没有这个变量,就会到index里去找 - 但是js这边不是,我贴的代码就是为了和java一样

@gogoyqj 了解了,近期修复掉,或者你发个 mr ?