How to fix panic: template: index.html:36: unexpected “.mytmpl” in template clause?


(Sam Temp) #1

When I define a template like {{define “home”}}{{end}} and use it inside my html file like {{template “home”}} it works fine. But When I do the same thing but using the template as a variable it shows the panic. For example, from go code I am sending a variable “mytmpl”: “home” and using it inside {{template .mytmpl}} and the “home” is also defined. But getting the panic.

// go code
gin.H{
    "title":    "Home",
    "username": name,
    "mytmpl":  "home",
},
<!-- html template -->
<body>
<div>
{{template .mytmpl}} // here is the panic
</div>
</body>

(Norbert Melzer) #2

Because it needs to be a literal string statically known at compiletime (the templates compiletime). At least thats how I would read the documentation, as every example uses literal strings.


( Kvaz1r) #3

Can you provide full code for reproducing the error and describe what exactly you are trying to achieve?

There is good articles about go-templates, maybe you will found something useful:
An Intro to Templates in Go - Contextual Encoding


(system) closed #4

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.