Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define变量的引用问题 #23

Open
gogoyqj opened this issue Sep 15, 2014 · 5 comments
Open

define变量的引用问题 #23

gogoyqj opened this issue Sep 15, 2014 · 5 comments

Comments

@gogoyqj
Copy link

gogoyqj commented Sep 15, 2014

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更加吻合

@fool2fish
Copy link
Owner

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

@lmf
Copy link

lmf commented May 7, 2015

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

@lmf
Copy link

lmf commented May 7, 2015

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

@gogoyqj
Copy link
Author

gogoyqj commented Jun 4, 2015

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

@fool2fish
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants