On Fri, 2026-03-13 at 10:39 -0700, Touring Tim wrote:
> I want to change the tags from H1 to H2 dynamically based upon if
> something is a report or sub-report.
>
> However even though the variable I use is a template.HTML which is
> meant to be safe, still GO is auto escaping stuff.
>
> Thanks in advance
> Tim
> -----------------------
>
> package main
>
> import (
> "html/template"
> "os"
> )
>
> type PageData struct {
> HeadingLevel template.HTML // e.g., "h1" or "h2"
> Title string
> }
>
> func main() {
> // 1. Define the template with a variable tag
> tmpl := `
> <{{.HeadingLevel}}>{{.Title}}</{{.HeadingLevel}}>
> `
> t := template.Must(template.New("index").Parse(tmpl))
>
> // 2. Data to pass to the template
> data := PageData{
> HeadingLevel: template.HTML("h1"), // This determines the tag
> Title: "Hello, World!",
> }
>
> // 3. Execute
> t.Execute(os.Stdout, data)
> }
The "<" is not a template.HTML in your code. Try something like this,
https://go.dev/play/p/zcgKpSI5stw.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/97c7fa124d84f334996a414c9c05f7ebfb86a8df.camel%40kortschak.io.