--- name: element-html-builder description: Element is a zero dependency library to efficiently generate HTML, without templates in Go --- # Element - AI Agent Documentation > Programmatic HTML generation in Go without templates. ## Overview Element is a zero-dependency Go library that generates HTML programmatically by leveraging Go's natural function execution order. Instead of templates, you write Go code that mirrors HTML's tree structure. ## Benefits 1. You never leave the safety, speed, and familiarity of Go - Compiles single-pass with the rest of your Go program -- no extra annotations or build steps - All of Go is available at any point in your code 2. Zero dependencies 3. Buffer pools for super-high traffic situations 4. Go's formatting naturally follows the HTML tree structure ## Installation ```bash go get -u github.com/rohanthewiz/element ``` ## General Strategy 1. Write the opening tag with attributes 2. Render the children and closing tag ## Core Concepts ### The Builder Pattern All HTML generation flows through a `Builder` instance. The builder maintains an internal buffer where tags are written immediately upon method calls. ### Immediate Tag Writing **Key Insight:** When you call a builder method like `b.Div()`, the opening tag `
` is written **immediately** to an internal buffer. The returned element must be **terminated** with `R()`, `T()`, or `F()` to write the closing tag. ```go b := element.NewBuilder() b.Div("id", "container") // "
" is NOW in the buffer // The element is returned, waiting for termination ``` Go's function argument evaluation order means children are processed (and their tags written) before the parent's `R()` closes the parent tag. This is what makes nested structures work correctly. ### Terminate all tags Every non-self-closing element **must** be terminated with `R()`, `T()`, or `F()`. Self-closing elements like `Br`, `Img`, `Input`, `Hr`, and `Meta` don't require termination, but to keep things consistent, close these tags with a sole `R()` or `T()`. ## Basic Usage ### Creating a Builder ```go import "github.com/rohanthewiz/element" // Standard creation b := element.NewBuilder() // Shorthand (equivalent) b := element.B() // From pool (for high-throughput HTTP handlers) b := element.AcquireBuilder() defer element.ReleaseBuilder(b) ``` ### Simple Elements ```go b := element.B() // Element with text content b.P().T("Hello, World!") // Output:

Hello, World!

// Element with children b.Div().R( b.P().T("First paragraph"), b.P().T("Second paragraph"), ) // Output:

First paragraph

Second paragraph

// Empty element b.Div().R() // Output:
// Get the HTML string html := b.String() ``` ### Complete Page Example ```go b := element.B() b.Html().R( b.Head().R( b.Title().T("My Page"), b.Meta("charset", "utf-8"), ), b.Body().R( b.DivClass("container").R( b.H1().T("Welcome"), b.P().T("Hello from Element!"), ), ), ) html := b.String() ``` ### Building with Multiple Functions ```go func main() { b := element.B() b.DivClass("container").R( b.H2().T("Section 1"), generateList(b), ) fmt.Println(b.Pretty()) /* // Output:

Section 1

  • Item 1
  • Item 2
*/ } func generateList(b *element.Builder) (x any) { // we don't care about the return // Everything is rendered in the builder immediately! b.Ul().R( b.Li().T("Item 1"), b.Li().T("Item 2"), ) return } ``` ## API Reference ### Builder Creation | Function | Description | | --------------------------- | --------------------------------------- | | `element.NewBuilder()` | Create a new builder | | `element.B()` | Shorthand for NewBuilder() | | `element.AcquireBuilder()` | Get builder from pool (high-throughput) | | `element.ReleaseBuilder(b)` | Return builder to pool | ### Termination Methods | Method | Use Case | Example | | -------------------- | ---------------------------------- | -------------------------- | | `R(children...)` | Elements with children or empty | `b.Div().R(b.P().T("hi"))` | | `T(strings...)` | Text-only content (most efficient) | `b.P().T("Hello")` | | `F(format, args...)` | Formatted text (like fmt.Sprintf) | `b.P().F("Count: %d", 42)` | ### Element Methods All standard HTML elements are available: `Div`, `Span`, `P`, `A`, `H1`-`H6`, `Ul`, `Ol`, `Li`, `Table`, `Tr`, `Td`, `Th`, `Form`, `Input`, `Button`, `Label`, `Img`, `Br`, `Hr`, `Meta`, `Link`, `Script`, `Style`, and 90+ more. ### Class Convenience Methods Most elements have a `*Class` variant where the first argument is the class attribute: ```go b.DivClass("container") //
b.PClass("intro", "id", "p1") //

b.ButtonClass("btn primary") //