Documentation menu

Tags and Attributes

Build HTML with tag and attribute helpers from github.com/franchb/htmgo/framework/v2/h. Tag helpers return *h.Element and accept attributes and child elements.

Copy
1h.Div(
2	h.Class("flex gap-2"),
3	h.Button(
4		h.Text("Submit"),
5	),
6)

h.Tag and h.Attribute are available to use when you need to render a tag or attribute that htmgo does not provide a method for.

Copy
1h.Tag("my-custom-tag",
2	h.Class("flex gap-2"),
3	h.Button(
4		h.Attribute("x-custom-attr", "my-value"),
5		h.Text("Submit"),
6	),
7)

Attributes are one of the main ways we can add interactivity to the pages with htmx.

htmgo provides various methods to add attributes to elements, as well as adding attributes based on a condition.

Copy
1h.Div(
2	h.Class("flex gap-2"),
3	h.Id("my-div"),
4	h.ClassIf(errorMessage != "", "p-4 bg-rose-400 text-white rounded"),
5)
The error class is added when errorMessage is nonempty.

This button sends the input named q to GET /search and places the returned fragment in #search-results. Your server supplies the search handler.

Copy
 1h.Div(
 2	h.Input("text",
 3		h.Id("search-query"),
 4		h.Name("q"),
 5		h.Placeholder("Search"),
 6	),
 7	h.Button(
 8		h.Type("button"),
 9		h.Get("/search"),
10		h.HxInclude("#search-query"),
11		h.HxTarget("#search-results"),
12		h.Text("Search"),
13	),
14	h.Div(h.Id("search-results")),
15)