htmlgo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 4 Imported by: 0

README

htmlgo

htmlgo is a small fluent HTML builder for Go. It escapes text and attribute values by default, supports raw HTML when you need it, and keeps the API focused on chainable element builders.

Install

go get github.com/aidenlabx/htmlgo@latest

Quick start

package main

import (
	"fmt"

	"github.com/aidenlabx/htmlgo"
)

func main() {
	page := htmlgo.HTML5(
		htmlgo.Head(
			htmlgo.Title(htmlgo.Text("Demo")),
		),
		htmlgo.Body(
			htmlgo.Div().
				ID("app").
				AddClass("container").
				Style("padding: 16px").
				Text("Hello <world>"),
		),
	)

	fmt.Println(page.String())
}

Highlights

  • Escapes text and attribute values by default.
  • Uses chainable builders for attributes, classes, styles, and children.
  • Renders HTML5 doctype with HTML5(...).
  • Provides Raw(...) when you want to inject trusted HTML.

Example

Run the bundled example with:

go run ./examples/basic

Release

See RELEASE.md for the suggested steps to publish a new version. For the first public release, see RELEASE_NOTES_v0.1.0.md.

Changelog

See CHANGELOG.md for version history.

License

This project is licensed under the MIT License. See LICENSE for details.

Documentation

Overview

Package htmlgo provides a fluent HTML builder with escaping by default.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element represents an HTML element with chainable helpers.

func A

func A(children ...Node) *Element

A creates an a element.

func Article

func Article(children ...Node) *Element

Article creates an article element.

func Body

func Body(children ...Node) *Element

Body creates a body element.

func Br

func Br(children ...Node) *Element

Br creates a br element.

func Button

func Button(children ...Node) *Element

Button creates a button element.

func Div

func Div(children ...Node) *Element

Div creates a div element.

func Footer(children ...Node) *Element

Footer creates a footer element.

func Form

func Form(children ...Node) *Element

Form creates a form element.

func HTML

func HTML(children ...Node) *Element

HTML creates the root html element.

func HTML5

func HTML5(children ...Node) *Element

HTML5 creates a complete HTML5 root with a doctype.

func Head(children ...Node) *Element

Head creates a head element.

func Header(children ...Node) *Element

Header creates a header element.

func Hr

func Hr(children ...Node) *Element

Hr creates an hr element.

func Img

func Img(children ...Node) *Element

Img creates an img element.

func Input

func Input(children ...Node) *Element

Input creates an input element.

func Label

func Label(children ...Node) *Element

Label creates a label element.

func Li

func Li(children ...Node) *Element

Li creates a li element.

func Link(children ...Node) *Element

Link creates a link element.

func Main

func Main(children ...Node) *Element

Main creates a main element.

func Meta

func Meta(children ...Node) *Element

Meta creates a meta element.

func Nav(children ...Node) *Element

Nav creates a nav element.

func Ol

func Ol(children ...Node) *Element

Ol creates a ol element.

func P

func P(children ...Node) *Element

P creates a p element.

func Script

func Script(children ...Node) *Element

Script creates a script element.

func Section

func Section(children ...Node) *Element

Section creates a section element.

func Span

func Span(children ...Node) *Element

Span creates a span element.

func Style

func Style(children ...Node) *Element

Style creates a style element.

func Table

func Table(children ...Node) *Element

Table creates a table element.

func Tag

func Tag(name string, children ...Node) *Element

Tag creates a generic HTML element.

func Tbody

func Tbody(children ...Node) *Element

Tbody creates a tbody element.

func Td

func Td(children ...Node) *Element

Td creates a td element.

func Textarea

func Textarea(children ...Node) *Element

Textarea creates a textarea element.

func Tfoot

func Tfoot(children ...Node) *Element

Tfoot creates a tfoot element.

func Th

func Th(children ...Node) *Element

Th creates a th element.

func Thead

func Thead(children ...Node) *Element

Thead creates a thead element.

func Title

func Title(children ...Node) *Element

Title creates a title element.

func Tr

func Tr(children ...Node) *Element

Tr creates a tr element.

func Ul

func Ul(children ...Node) *Element

Ul creates a ul element.

func (*Element) AddClass

func (e *Element) AddClass(names ...string) *Element

AddClass appends one or more CSS classes without duplicates.

func (*Element) After

func (e *Element) After(nodes ...Node) *Element

After appends nodes that render after the closing tag.

func (*Element) Alt

func (e *Element) Alt(value string) *Element

Alt sets the alt attribute.

func (*Element) Append

func (e *Element) Append(nodes ...Node) *Element

Append appends child nodes.

func (*Element) Attr

func (e *Element) Attr(key, value string) *Element

Attr sets or replaces a string attribute.

func (*Element) Before

func (e *Element) Before(nodes ...Node) *Element

Before prepends nodes that render before the opening tag.

func (*Element) Data

func (e *Element) Data(key, value string) *Element

Data sets a data-* attribute.

func (*Element) Href

func (e *Element) Href(value string) *Element

Href sets the href attribute.

func (*Element) ID

func (e *Element) ID(value string) *Element

ID sets the id attribute.

func (*Element) Prepend

func (e *Element) Prepend(nodes ...Node) *Element

Prepend prepends child nodes.

func (*Element) Rel

func (e *Element) Rel(value string) *Element

Rel sets the rel attribute.

func (*Element) RemoveAttr

func (e *Element) RemoveAttr(key string) *Element

RemoveAttr removes all occurrences of an attribute.

func (*Element) RemoveClass

func (e *Element) RemoveClass(names ...string) *Element

RemoveClass removes one or more CSS classes.

func (*Element) Src

func (e *Element) Src(value string) *Element

Src sets the src attribute.

func (*Element) String

func (e *Element) String() string

String renders the element to HTML.

func (*Element) Style

func (e *Element) Style(declarations ...string) *Element

Style appends CSS declarations that are merged at render time.

func (*Element) TabIndex

func (e *Element) TabIndex(value int) *Element

TabIndex sets the tabindex attribute.

func (*Element) Text

func (e *Element) Text(value string) *Element

Text appends escaped text content.

func (*Element) Void

func (e *Element) Void() *Element

Void marks the element as a void element.

type Node

type Node interface {
	String() string
}

Node is anything that can be rendered to HTML.

func Doctype

func Doctype() Node

Doctype returns the HTML5 doctype node.

func Raw

func Raw(value string) Node

Raw returns a node that is inserted without HTML escaping.

func Text

func Text(value string) Node

Text returns an escaped text node.

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL