Navigation Menu

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

html/template: clarify "ambiguous URL context" message #17319

Closed
bep opened this issue Oct 2, 2016 · 2 comments
Closed

html/template: clarify "ambiguous URL context" message #17319

bep opened this issue Oct 2, 2016 · 2 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@bep
Copy link
Contributor

bep commented Oct 2, 2016

go version go1.7 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bep/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n6/s_85mm8d31j6yctssnmn_g1r0000gn/T/go-build506557116=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
package main

import (
    "fmt"
    "html/template"
    "io/ioutil"
    "log"
    "path"
    "strings"
)

type A struct {
    I []string
}

func main() {
    for i, tpl := range tpls {
        t, err := template.New("foo").Funcs(funcs).Parse(tpl)

        if err != nil {
            log.Fatal(err)
        }

        a := A{I: []string{"abc"}}

        err = t.Execute(ioutil.Discard, a)

        if err != nil {
            fmt.Printf("%d failed: %s\n", i, err)
        }
    }
}

var tpls = []string{
    // Without surrounding div executes OK
    `{{ range .I }}{{ if in . "a" }}{{ join . "a.jpg" }}{{ else  }}a.jpg{{ end }}{{ end }}`,
    // This also works fine
    `<div style="background: url({{ range .I }}{{ if in . "a" }}ab.jpg{{ else  }}a.jpg{{ end }}{{ end }})">FOO</div>`,
    // This fails
    `<div style="background: url({{ range .I }}{{ if in . "a" }}{{ join . "a.jpg" }}{{ else  }}a.jpg{{ end }}{{ end }})">FOO</div>`,
}
var funcs = map[string]interface{}{
    "join": func(a, b string) string { return path.Join(a, b) },
    "in":   func(a, b string) bool { return strings.Contains(a, b) },
}

The above prints:

2 failed: html/template:foo:1:62: on range loop re-entry: {{join . "a.jpg"}} appears in an ambiguous URL context

I would have expected it to not error.

@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 3, 2016
@quentinmit quentinmit added this to the Go1.8Maybe milestone Oct 3, 2016
@rsc
Copy link
Contributor

rsc commented Oct 19, 2016

Dup of #2644, but I would still like to update the message as originally suggested there to avoid future confusion.

That is, I'd like to see

{.Name} appears in an ambiguous URL context

change to:

{.Name} appears in an ambiguous context: either beginning or middle of URL

Working on some bigger template issues right now but once those are out I will look into this.

@rsc rsc modified the milestones: Go1.8, Go1.8Maybe Oct 19, 2016
@rsc rsc changed the title html/template: on range loop re-entry: appears in an ambiguous URL context html/template: clarify "ambiguous URL context" message Oct 19, 2016
@gopherbot
Copy link

CL https://golang.org/cl/31465 mentions this issue.

@golang golang locked and limited conversation to collaborators Oct 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants