cfatt10
cfatt10

Reputation: 788

Variable not accessible inside if statement. Language design?

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.

Upvotes: 0

Views: 93

Answers (1)

Herman Schaaf
Herman Schaaf

Reputation: 48425

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?

Upvotes: 2

Related Questions