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

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>
1 Like

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.

1 Like

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

1 Like

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