예제 #1
0
func Example() {
	// Context-aware escaping is done just like in html/template.
	html := htmlg.Render(
		htmlg.Text("Hi & how are you, "),
		htmlg.A("Gophers", "https://golang.org/"),
		htmlg.Text("? <script> is a cool gopher."),
	)

	fmt.Fprintln(os.Stdout, html)

	// Output:
	// Hi &amp; how are you, <a href="https://golang.org/">Gophers</a>? &lt;script&gt; is a cool gopher.
}
예제 #2
0
파일: backend.go 프로젝트: beyang/frontend
// New creates the HTML for a select menu instance with the specified parameters.
func New(options []string, defaultOption string, query url.Values, queryParameter string) template.HTML {
	selectElement := &html.Node{Type: html.ElementNode, Data: "select"}

	var selectedOption = defaultOption
	if query.Get(queryParameter) != "" {
		selectedOption = query.Get(queryParameter)
	}
	if !contains(options, selectedOption) {
		options = append(options, selectedOption)
	}
	for _, option := range options {
		o := &html.Node{Type: html.ElementNode, Data: "option"}
		o.AppendChild(htmlg.Text(option))
		if option == selectedOption {
			o.Attr = append(o.Attr, html.Attribute{Key: "selected"})
		}
		selectElement.AppendChild(o)
	}

	selectElement.Attr = append(selectElement.Attr, html.Attribute{
		Key: "oninput",
		// HACK: Don't use Sprintf, properly encode (as json at this time).
		Val: fmt.Sprintf(`SelectMenuOnInput(event, this, %q, %q);`, strconv.Quote(defaultOption), strconv.Quote(queryParameter)),
	})

	return htmlg.Render(selectElement)
}