Getting Started
Core Concepts
Control
Interactivity
Performance
Pushing Data
HTMX Extensions
Miscellaneous
Configuration
Partials
Partials are where things get interesting.
Partials allow you to start adding interactivity to your website by swapping in content, setting headers, redirecting, etc.
Partials have a similar structure to pages. A simple partial may look like:
1func CurrentTimePartial(ctx *h.RequestContext) *h.Partial {
2 now := time.Now()
3 return h.NewPartial(
4 h.Div(
5 h.Pf("The current time is %s", now.Format(time.RFC3339)),
6 ),
7 )
8}
Simple Example
I want to build a page that renders the current time, updating every second. Here is how that may look:
pages/time.go
1func CurrentTimePage(ctx *h.RequestContext) *h.Page {
2 return RootPage(
3 h.GetPartial(partials.CurrentTimePartial, "load, every 1s")
4 )
5}
partials/time.go
1func CurrentTimePartial(ctx *h.RequestContext) *h.Partial {
2 now := time.Now()
3 return h.NewPartial(
4 h.Div(
5 h.Pf("The current time is %s", now.Format(time.RFC3339)),
6 ),
7 )
8}
When the page load, the partial will be loaded in via htmx, and then swapped in every 1 second.
With this little amount of code and zero written javascript, you have a page that shows the current time and updates every second.