shiyanhui / hero

A handy, fast and powerful go template engine.
https://shiyanhui.github.io/hero
Other
1.57k stars 96 forks source link

Declaring a type in the import block of an include will duplicate that type declaration when the include is used in multiple templates. #83

Open abalbanyan opened 4 years ago

abalbanyan commented 4 years ago

I'd like to be able to declare types in my partial files.

partial.html

<%!
  type MyPartial struct {
      Name string
  }
%>

templateA.html

<%: func TemplateA(data []string, buffer *bytes.Buffer) %>

<%+ "partial.html" %>

...

templateB.html

<%: func TemplateB(data []string, buffer *bytes.Buffer) %>

<%+ "partial.html" %>

...

The generated templates, templateB.html.go and templateA.html.go will both have the MyPartial type declaration, which will cause the MyPartial redeclared in this block golang error.

One potential solution to this issue would be to add a new syntax dedicated to type declarations. The types declared using this new syntax would only be output once.

Let me know if there's an alternative way to accomplish this that I'm missing. I'm also willing to submit a PR if fixing this issue is non-trivial to accomplish. Thank you very much!