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

Open
gkarthiks opened this issue Jul 4, 2019 · 4 comments

Comments

@gkarthiks
Copy link

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

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

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

@bcmills

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

Please provide a complete example that illustrates the problem.

@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

This comment has been minimized.

Copy link
Author

commented Jul 9, 2019

@bcmills will be updating the complete data shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.