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 : Errors with parseFiles and Funcs #31935

Closed
LouisBruge opened this issue May 9, 2019 · 1 comment
Closed

html/template : Errors with parseFiles and Funcs #31935

LouisBruge opened this issue May 9, 2019 · 1 comment

Comments

@LouisBruge
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.12.4 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/louis/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/louis/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/louis/go/src/git.pingcode.io/pingauth/services/svc_users/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build574375994=/tmp/go-build -gno-record-gcc-switches"

What did you do?

import (
	"html/template"
	"github.com/nicksnyder/go-i18n/i18n" // tag 1.10.0
)

main() {
i18n.MustLoadTranslationFile("fr.json")
T, _ := i18n.Tfunc(language.French.String(), "fr.json")
	//fmt.Printf("%s", T)
	t, err := template.New("foo").Funcs(map[string]interface{}{"T": T}).ParseFiles("alert.html", "model.html")

	if err != nil {
		return err
	}

	buffer := new(bytes.Buffer)
	if err = t.ExecuteTemplate(buffer, "foo", r.data); err != nil {
		return err
	}
}
  • fr.json
[
  {
    "id": "accountLockUntil_message",
    "translation": "foobar  :"
  }
]
  • model.html
{{ define "model" }}
    <!DOCTYPE HTML>
    <html>
    <head>
        {{ block "title" . }}{{ end }}
    </head>
    <body>
    {{ block "content" . }}{{ end }}
    </body>
    </html>
{{ end }}
  • alert.html
{{ template "model" . }}
{{ define "title" }}
    <title>{{ .Username }}</title>
{{ end }}

{{ define "content" }}
    <p>{{ T "accountLockUntil_message"}}</p>
    <p>{{ .Username}}</p>
    <p>{{.SiteUrl}}</p>
    <p>{{.RecoveryUrl}}</p>
{{ end }}

What did you expect to see?

Success to parse template split into two files and translate it with T func provide by go-i18n

What did you see instead?

Unexpected error : html/template: "foo" is an incomplete template

@LouisBruge
Copy link
Author

Revolve

You should pass the filename (with extension) of template to parse, here, alert.html to template.New and template.ExecuteTemplate (source : #11247 (comment))

so, this code should work

import (
	"html/template"
	"github.com/nicksnyder/go-i18n/i18n" // tag 1.10.0
)

main() {
i18n.MustLoadTranslationFile("fr.json")
T, _ := i18n.Tfunc(language.French.String(), "fr.json")
	//fmt.Printf("%s", T)
	t, err := template.New("alert.html").Funcs(map[string]interface{}{"T": T}).ParseFiles("alert.html", "model.html")

	if err != nil {
		return err
	}

	buffer := new(bytes.Buffer)
	if err = t.ExecuteTemplate(buffer, "alert.html", r.data); err != nil {
		return err
	}
}

@golang golang locked and limited conversation to collaborators May 8, 2020
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