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: template.HTMLAttr value garbled in style attribute #27178

Closed
bep opened this issue Aug 23, 2018 · 1 comment
Closed

html/template: template.HTMLAttr value garbled in style attribute #27178

bep opened this issue Aug 23, 2018 · 1 comment

Comments

@bep
Copy link
Contributor

bep commented Aug 23, 2018

▶ go version && go env
go version go1.11rc2 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bep/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bep/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n6/s_85mm8d31j6yctssnmn_g1r0000gn/T/go-build909831316=/tmp/go-build -gno-record-gcc-switches -fno-common"
▶ uname -a
Darwin MacBook-Pro.local 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 x86_64
package main

import (
	"bytes"
	"html/template"
	"log"
	"strings"
)

func main() {
	data := map[string]interface{}{
		"style": template.HTMLAttr("background-image: url(../images/logo.png)"),
	}
	tpl := `
<section style="{{ .style }}">
</section>
`

	var buf bytes.Buffer
	tmpl, err := template.New("").Parse(tpl)
	if err != nil {
		log.Fatal(err)
	}

	if err := tmpl.Execute(&buf, data); err != nil {
		log.Fatal(err)
	}

	result := strings.TrimSpace(buf.String())

	if !strings.Contains(result, "background-image") {
		log.Fatal(result)
	}

}

The above program prints:

<section style="ZgotmplZ">
</section>
@bep
Copy link
Contributor Author

bep commented Aug 23, 2018

Using template.CSSworks, which also is the correct for my use case. Closing this.

package main

import (
	"bytes"
	"html/template"
	"log"
	"strings"
)

func main() {
	data := map[string]interface{}{
		"style": template.CSS("background-image: url(../images/logo.png)"),
	}
	tpl := `
<section style="{{ .style }}">
</section>
`

	var buf bytes.Buffer
	tmpl, err := template.New("").Parse(tpl)
	if err != nil {
		log.Fatal(err)
	}

	if err := tmpl.Execute(&buf, data); err != nil {
		log.Fatal(err)
	}

	result := strings.TrimSpace(buf.String())

	if !strings.Contains(result, "background-image") {
		log.Fatal(result)
	}

}

@bep bep closed this as completed Aug 23, 2018
@golang golang locked and limited conversation to collaborators Aug 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants