Quick actions

cmd+k|ctrl+k

Navigation

Languages

Text and HTML templates / HTML templates / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:16:51Z

Updated

2019-03-26T10:16:51Z

package main

import (
	"fmt"
	"html/template"
	"log"
	"os"
)

const tmplStr = `<div onlick="{{ .JS }}">{{ .HTML }}</div>
`

func main() {

	html := template.Must(template.New("html").Parse(tmplStr))

	data := struct {
		JS   string
		HTML string
	}{
		JS:   `foo`,
		HTML: `<span>text</span>`,
	}

	fmt.Printf("Escaped:\n")
	err := html.Execute(os.Stdout, data)
	if err != nil {
		log.Fatalf("t.Execute() failed with '%s'\n", err)
	}

	fmt.Printf("\nUnescaped:\n")
	data2 := struct {
		JS   template.JS
		HTML template.HTML
	}{
		JS:   `foo`,
		HTML: `<span>text</span>`,
	}
	err = html.Execute(os.Stdout, data2)
	if err != nil {
		log.Fatalf("t.Execute() failed with '%s'\n", err)
	}

}
INFO