duanli12176 2016-02-20 00:31
浏览 62
已采纳

if语句内部无法访问变量。 语言设计?

So I'm working with an implementation of the Jade templating language for Go (see https://github.com/go-floki/jade) and I'm running into an interesting "feature" of the language. The code below works as expected, placing and img element for every headshot.

each $headshot in $object.Headshots
    img.img-circle.headshot(src=$headshot)

I then wanted to change it so on the sixth element the image source would be a preset image. However, when I run this code I get an error

each $headshot, index in $cause.Headshots
    if index == 6
        img.img-circle.headshot(src="/public/images/ellipse.png")
    else
        img.img-circle.headshot(src=$headshot)

Specifically undefined variable $headshot. It seems that $headshot does not exist within the scope of the if statement. This isn't the first time I've run into this behavior using this implementation and it's frustrating to try to work around. The trouble that it takes made me wonder, is there possibly a reason the language works this way?

Additionally, can anyone think of a way to work around the "feature" in this case? The best I can come up with is to change it later client-side with Javascript.

  • 写回答

1条回答 默认 最新

  • doujing5937 2016-02-20 07:55
    关注

    First off, Go's if blocks have access to variables in their enclosing scope. If this is failing in your example, it must be because of an implementation error either in your code or the library you are using.

    Next, let's fix some problems in the posted code:

    each $headshot, index in $cause.Headshots
    

    The order should be reversed--index goes first--and let's be consistent with the use of $ to indicate variables:

    each $i, $headshot in $cause.Headshots
    

    With that cleared up, here is a full demo script:

    templates/home.jade

    html
        body
            each $i, $headshot in Cause.Headshots
                if $i == 0
                    img.img-circle.headshot(src="/public/images/ellipse.png")
                else
                    img.img-circle.headshot(src=$headshot)
    

    demo.go

    package main
    
    import (
        "bufio"
        "os"
    
        "github.com/go-floki/jade"
    )
    
    func main() {
        w := bufio.NewWriter(os.Stdout)
        // compile templates
        templates, err := jade.CompileDir("./templates", jade.DefaultDirOptions, jade.Options{})
        if err != nil {
            panic(err)
        }
    
        // then render some template
        tpl := templates["home"]
        tpl.Execute(w, map[string]interface{}{
            "Cause": map[string]interface{}{
                "Headshots": []string{"one", "two"},
            },
        })
        w.Flush()
    }
    

    This code works for me, the output is:

    <html><body><img class="img-circle headshot" src="/public/images/ellipse.png" /><img class="img-circle headshot" src="two" /></body></html>
    

    So my only conclusion is that there must be something else going on in your example. It could be a bug in the library, but I would first check the following things:

    • is there a mixture of spaces and tabs in the jade file? This could cause scope confusion
    • does the example I posted above also give you an error? If so,
      • are you using the latest version of the Jade library?
      • is your Go version up to date?
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据