go-echarts / go-echarts

🎨 The adorable charts library for Golang

Home Page:https://go-echarts.github.io/go-echarts/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using `render.MustTemplate` with chart causes parsing error

LJ-Software opened this issue · comments

When attempting to use the base template with my chart I get this template parsing error function "safeJS" not defined. This led me to see that the render.MustTemplate function does not add the function safeJS to the template before calling parse on the template.

https://pkg.go.dev/text/template#Template.Funcs
States that

Funcs adds the elements of the argument map to the template's function map. It must be called before the template is parsed.

I've tested making the tweak to render.MustTemplate to abide by this requirement and it works correctly now.

Hi @LJ-Software , could plz provide a tiny reproduce demo? thx.

@Koooooo-7 Sure, I was able to get the same error with this code:

package main

import (
  "bytes"
  "log"
  
  "github.com/go-echarts/go-echarts/v2/charts"
  "github.com/go-echarts/go-echarts/v2/render"
  "github.com/go-echarts/go-echarts/v2/templates"
)

func main() {
  graph := charts.NewGraph()
  contents := []string{templates.BaseTpl, templates.ChartTpl, templates.HeaderTpl}
  tpl := render.MustTemplate("chart", contents) 
  buf := new(bytes.Buffer)
  if err := tpl.ExecuteTemplate(buf, "chart", graph); err != nil { 
    log.Panic("could not execute template")
  }
}

the error:

panic: template: chart:9: function "safeJS" not defined

goroutine 1 [running]:
html/template.Must(...)
        /usr/local/Cellar/go/1.20.3/libexec/src/html/template/template.go:368
github.com/go-echarts/go-echarts/v2/render.MustTemplate({0x1196f25?, 0xc0000061a0?}, {0xc000151f40?, 0x3?, 0x3?})
        /Users/ljones/go/pkg/mod/github.com/go-echarts/go-echarts/v2@v2.2.6/render/engine.go:108 +0x237
main.main()
        /Users/ljones/Development/echarts-bug-min-example/main.go:15 +0xab
exit status 2

Would it be possible to get a new release tag off the master branch with this fix in? @Koooooo-7

Would it be possible to get a new release tag off the master branch with this fix in? @Koooooo-7

No prob, I gonna have a patch release on it, and you could tag the commit id as workaround.

FYI @LJ-Software , v2.2.7 released.