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: user-defined function is not parsed via ParseFiles #32934

Closed
gkarthiks opened this issue Jul 4, 2019 · 5 comments
Closed

html/template: user-defined function is not parsed via ParseFiles #32934

gkarthiks opened this issue Jul 4, 2019 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@gkarthiks
Copy link

gkarthiks commented Jul 4, 2019

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

$ go version
go version go1.12.6 darwin/amd64

Does this issue reproduce with the latest release?

Yes, its with 1.12 release

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kagovind/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kagovind/GODeveloper"
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="/Users/kagovind/GODeveloper/data-fabric-reconcile-alerter/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/yz/mp010ls16zdcnm10w218zywxvykr04/T/go-build353080250=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have a HTML template; Along with all the CSS and HTML tags, below is the template I want to iterate over. I have stored this as a file in HTML format.

    <tr>
        <td style="padding: 30px 0 20px 0;" bgcolor="#dad8e6">
            <table bgcolor="#e1effc" cellspacing="0" class="buttonwrapper" cellspacing="0" celpadding="0" border=".5">
                <tr bgcolor="#3662d1" style="color: white">
                    <th>S No</th>
                    <th>Unused </th>
                </tr>
                {{ range $index, $element := .UnUsed }}
                    <tr>
                        <td>
                            {{ $index | inc }}
                        </td>
                        <td>
                            {{ $element }}
                        </td>
                    </tr>
                {{ end }}
            </table>
        </td>
    </tr>

Now I have the following snippet of code for incrementing the Index in the template.

func inc(i int) int {
    return i + 1
}
funcMap := template.FuncMap{
    "inc": inc,
}

tmpl = template.New("email").Funcs(funcMap)
tmpl, err = tmpl.ParseFiles(templateFileName)

if err != nil {
    log.Fatalf("Error occurred while parsing the template %s ", err)
} else {
    log.Debug("Parsing the html template completed")
}
buffer := new(bytes.Buffer)
if err = tmpl.Execute(buffer, data); err != nil {
    log.Fatalf("Error occurred while parsing the template %s ", err)
}

When I execute the above code, nothing is getting parsed. But if I parse the same template passed to the template as a string like below

fileBytes, err := ioutil.ReadFile(templateFileName)
if err != nil {
    log.Fatalf("Error while reading file data into byte array %s ",err)
}

funcMap := template.FuncMap{
    "inc": inc,
}
tmpl, err := template.New("email").Funcs(funcMap).Parse(string(fileBytes))

this later one via []byte is working.

What did you expect to see?

Should have the index incremented with +1

What did you see instead?

The template file was not parsed.

@bcmills
Copy link
Contributor

bcmills commented Jul 8, 2019

When possible, please post code examples as runnable links to https://play.golang.org rather than isolated snippets.

@bcmills
Copy link
Contributor

bcmills commented Jul 8, 2019

Note that both of your log.Fatalf calls have the same error message. Correcting that (as https://play.golang.org/p/wgWilb_xu0m), I see:

2009/11/10 23:00:00 Parsing the html template completed
2009/11/10 23:00:00 Error occurred while executing the template: template: email: "email" is an incomplete or empty template

@bcmills
Copy link
Contributor

bcmills commented Jul 8, 2019

Please provide a complete example that illustrates the problem.

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 8, 2019
@bcmills bcmills changed the title userdefined function in html template is not parsed via ParseFiles html/template: user-defined function is not parsed via ParseFiles Jul 8, 2019
@gkarthiks
Copy link
Author

@bcmills will be updating the complete data shortly.

@ALTree
Copy link
Member

ALTree commented Jun 18, 2021

This has been in waiting for info for 2 years, it was reported against a version of Go that is no longer supported, and we still don't have an auto-contained reproducer, so let's archive this. If it comes out again on a recent Go version we can open a new issue.

@ALTree ALTree closed this as completed Jun 18, 2021
@golang golang locked and limited conversation to collaborators Jun 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants