html

package module
v0.16.16 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: Apache-2.0 Imports: 3 Imported by: 8

README

html 🚧

GoDoc Version Build Status Go Report Card Codecov

Efficient HTML Tag Assembly

This library provides functions for generating a string HTML tags.

b := html.New()

b.Div().Class("wrapper")
b.Div().Class("inner")
b.Form().Attr("action", "my-server")
b.Input().Name("FullName").Value("John Connor").Close()
b.Input().Name("Email").Value("john@connor.mil").Close()
b.CloseAll()
b.String()

Why Builder?

Why not just use Go Templates instead? Templates work great in many cases, but they can be cumbersome when building complex conditional logic directly in your code. Builder uses an efficient strings.Builder to assemble the exact HTML you need, and nothing extra.

Pull Requests Welcome

This library is growing rapidly, as the requirements of its downstram projects continue to evolve. How can it help you build your next masterpiece? Add your voice, because we're all in this together! 🚧

Documentation

Overview

Package html provides functions and types for efficiently building HTML documents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder added in v0.9.0

type Builder struct {
	*strings.Builder
	// contains filtered or unexported fields
}

Builder collects tags and attributes into a strings.Builder efficiently.

func New added in v0.9.0

func New() *Builder

New generates a fully initialized Builder

func (*Builder) A added in v0.10.1

func (b *Builder) A(href string) *Element

A creates a new anchor element with the specified href https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

func (*Builder) Audio added in v0.16.5

func (b *Builder) Audio() *Element

Audio creates a new abbreviation element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio

func (*Builder) B added in v0.11.2

func (b *Builder) B(class string) *Element

B creates a new bold element with the specified class https://developer.mozilla.org/en-US/docs/Web/HTML/Element/b

func (*Builder) BR added in v0.9.0

func (b *Builder) BR() *Element

BR creates a new line break element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br

func (*Builder) Body added in v0.16.4

func (b *Builder) Body() *Element

Body creates a new body element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body

func (*Builder) Button added in v0.10.0

func (b *Builder) Button() *Element

Button creates a new button element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

func (*Builder) Bytes added in v0.12.1

func (builder *Builder) Bytes() []byte

Bytes returns the assembled HTML as a slice of bytes.

func (*Builder) Close added in v0.9.0

func (builder *Builder) Close() *Builder

Close completes the last tag on the stack, then pops it off of the stack

func (*Builder) CloseAll added in v0.9.0

func (builder *Builder) CloseAll() *Builder

CloseAll calls .Cload() until the stack is empty.

func (*Builder) Container added in v0.9.0

func (builder *Builder) Container(name string) *Element

Container creates a new "container" element that WILL have an end tag.

func (*Builder) Datalist added in v0.10.0

func (b *Builder) Datalist(id string) *Element

Datalist creates a new datalist element with the specified ID https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist

func (*Builder) Div added in v0.9.0

func (b *Builder) Div() *Element

Div creates a new div element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div

func (*Builder) Element added in v0.9.0

func (builder *Builder) Element(name string, container bool) *Element

Element adds a new HTML element to the builder

func (*Builder) Empty added in v0.9.0

func (builder *Builder) Empty(name string) *Element

Empty creates a new "empty" or non-container element that WILL NOT have an end tag

func (*Builder) EndBracket added in v0.11.2

func (builder *Builder) EndBracket() *Builder

EndBracket adds an end bracket to the last tag on the stack

func (*Builder) FigCaption added in v0.16.7

func (b *Builder) FigCaption(innerHTML string) *Element

FigCaption creates a new figcaption element with the specified inner HTML https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption

func (*Builder) Figure added in v0.16.7

func (b *Builder) Figure() *Element

Figure creates a new figure element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure

func (*Builder) Form added in v0.11.0

func (b *Builder) Form(method string, action string) *Element

Form creates a new form element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

func (*Builder) H1 added in v0.11.2

func (b *Builder) H1() *Element

H1 creates a new h1 element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h1

func (*Builder) H2 added in v0.11.2

func (b *Builder) H2() *Element

H2 creates a new h2 element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h2

func (*Builder) H3 added in v0.11.2

func (b *Builder) H3() *Element

H3 creates a new h3 element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h3

func (*Builder) HTML added in v0.16.4

func (b *Builder) HTML() *Element

HTML creates a new html element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html

func (*Builder) Head added in v0.16.4

func (b *Builder) Head() *Element

Head creates a new head element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head

func (*Builder) I added in v0.11.2

func (b *Builder) I(classes ...string) *Element

I creates a new italics element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i

func (*Builder) Img added in v0.16.5

func (b *Builder) Img(src string) *Element

Img creates a new image element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img

func (*Builder) Input added in v0.9.0

func (b *Builder) Input(t string, name string) *Element

Input creates a new input element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

func (*Builder) Label added in v0.9.0

func (b *Builder) Label(forID string) *Element

Label creates a new label element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label

func (b *Builder) Link(rel string, href string) *Element

Link creates a new link element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link

func (*Builder) OptGroup added in v0.10.0

func (b *Builder) OptGroup(label string) *Element

OptGroup creates a new meta element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup

func (*Builder) Option added in v0.10.0

func (b *Builder) Option(label string, value string) *Element

Option creates a new option element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

func (*Builder) OptionSelected added in v0.16.6

func (b *Builder) OptionSelected(label string, value string) *Element

OptionSelected creates a new option element with the "selected" attribute set to true https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

func (*Builder) Picture added in v0.16.7

func (b *Builder) Picture() *Element

Picture creates a new picture element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture

func (*Builder) ReadString added in v0.12.1

func (builder *Builder) ReadString() string

ReadString reads the existing contents of the buffer but DOES NOT close any existing tags. This is useful for reading the header of an HTML document before calling a subroutine that will fill in its body.

func (*Builder) Reader added in v0.11.2

func (builder *Builder) Reader() io.Reader

Reader returns the string as an io.Reader.

func (*Builder) Script added in v0.11.0

func (b *Builder) Script() *Element

Script creates a new script element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

func (*Builder) Select added in v0.10.0

func (b *Builder) Select(name string) *Element

Select creates a new select element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

func (*Builder) Source added in v0.16.5

func (b *Builder) Source() *Element

Source creates a new source element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source

func (*Builder) Space added in v0.11.5

func (builder *Builder) Space() *Builder

Space adds a single space character to the buffer

func (*Builder) Span added in v0.9.0

func (b *Builder) Span() *Element

Span creates a new span element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span

func (*Builder) String added in v0.9.0

func (builder *Builder) String() string

String returns the assembled HTML as a string. It overrides the default behavior of the strings.Builder by also calling CloseAll() on all unclosed tags in the stack before generating HTML.

func (*Builder) Stylesheet added in v0.12.1

func (b *Builder) Stylesheet(url string) *Element

Stylesheet creates a new link element for a stylesheet https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link

func (*Builder) SubTree added in v0.9.0

func (builder *Builder) SubTree() *Builder

SubTree generates a new Builder that shares this Builder's string buffer. This is useful when sending a Builder to another function, so that the other function can maintain it's own stack of elements -- and potentially call .CloseAll() -- without affecting this current builder.

func (*Builder) TBody added in v0.14.1

func (b *Builder) TBody() *Element

TBody creates a new table body element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tbody

func (*Builder) TD added in v0.11.2

func (b *Builder) TD() *Element

TD creates a new table data element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td

func (*Builder) TH added in v0.11.2

func (b *Builder) TH() *Element

TH creates a new table header element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th

func (*Builder) TR added in v0.11.2

func (b *Builder) TR() *Element

TR creates a new table row element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr

func (*Builder) Table added in v0.11.2

func (b *Builder) Table() *Element

Table creates a new table element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table

func (*Builder) Textarea added in v0.10.0

func (b *Builder) Textarea(name string) *Element

Textarea creates a new textarea element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea

func (*Builder) Title added in v0.16.4

func (b *Builder) Title(value string) *Element

Title creates a new title element https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title

type Element added in v0.9.0

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

Element represents a element that is being written into the provided strings.Builder

func (*Element) Aria added in v0.11.3

func (element *Element) Aria(name string, value string) *Element

Aria adds a "aria-*" attribute to the Element

func (*Element) Attr added in v0.9.0

func (element *Element) Attr(name string, value string) *Element

Attr writes the attribute into the string builder. It converts the value (second parameter) into a string, and then uses html.EscapeString to escape the attribute value. Attribute names ARE NOT escaped. Empty values are not written to the builder.

func (*Element) Class added in v0.9.0

func (element *Element) Class(values ...string) *Element

Class adds a "class" attribute to the Element. Multiple class names can be passed, and are separated by spaces

func (*Element) Close added in v0.9.0

func (element *Element) Close() *Element

Close writes the necessary closing tag for this element and marks it closed

func (*Element) Data added in v0.11.1

func (element *Element) Data(name string, value string) *Element

Data adds a "data-" attribute to the Element

func (*Element) EndBracket added in v0.9.0

func (element *Element) EndBracket() *Element

EndBracket writes the final ">" of the beginning element to the strings.Builder It uses an internal variable to prevent duplicate calls

func (*Element) For added in v0.9.0

func (element *Element) For(value string) *Element

For adds a "for" attribute to the Element

func (*Element) ForceAttr added in v0.16.6

func (element *Element) ForceAttr(name string, value string) *Element

ForceAttr writes the attribute into the string builder. It converts the value (second parameter) into a string, and then uses html.EscapeString to escape the attribute value. Attribute names ARE NOT escaped. Empty values ARE written to the builder

func (*Element) Href added in v0.12.1

func (element *Element) Href(url string) *Element

Href adds an "href" attribute to the Element

func (*Element) HxGet added in v0.16.16

func (element *Element) HxGet(value string) *Element

HxGet adds a "hx-get" attribute to the Element

func (*Element) HxPost added in v0.16.16

func (element *Element) HxPost(value string) *Element

HxPost adds a "hx-post" attribute to the Element

func (*Element) HxSwap added in v0.16.16

func (element *Element) HxSwap(value string) *Element

HxSwap adds a "hx-swap" attribute to the Element

func (*Element) HxTarget added in v0.16.16

func (element *Element) HxTarget(value string) *Element

HxTarget adds a "hx-target" attribute to the Element

func (*Element) HxTrigger added in v0.16.16

func (element *Element) HxTrigger(value string) *Element

HxTrigger adds a "hx-trigger" attribute to the Element

func (*Element) ID added in v0.9.0

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

ID adds an "id" attribute to the Element

func (*Element) InnerHTML added in v0.9.0

func (element *Element) InnerHTML(innerHTML string) *Element

InnerHTML does three things: 1) closes the beginning element (if needed) 2) appends innerHTML (if provided) 3) writes an ending element to the builder (ie. </element> ) InnerHTML DOES NOT escape its content, so it is not safe to use with user-generated content.

func (*Element) InnerText added in v0.16.0

func (element *Element) InnerText(text string) *Element

InnerText works similarly to the InnerHTML method. In addition, it HTML escapes its content so that user-generated-content can be inserted into the output safely.

func (*Element) Label added in v0.10.0

func (element *Element) Label(value string) *Element

Label adds a "label" attribute to the Element

func (*Element) List added in v0.10.0

func (element *Element) List(value string) *Element

List adds a "list" attribute to the Element

func (*Element) Media added in v0.16.8

func (element *Element) Media(value string) *Element

Media adds a "name" attribute to the Element

func (*Element) Name added in v0.9.0

func (element *Element) Name(value string) *Element

Name adds a "name" attribute to the Element

func (*Element) Rel added in v0.12.1

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

Rel adds a "rel" (relationship) attribute to the Element (valid values listed at https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel)

func (*Element) Role added in v0.11.3

func (element *Element) Role(value string) *Element

Role adds a "role" attribute to the Element (for WAI-ARIA)

func (*Element) Script added in v0.10.0

func (element *Element) Script(value ...string) *Element

Script adds a "data-script" attribute to the Element (for https://hyperscript.org)

func (*Element) Src added in v0.12.1

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

Src adds a "src" attribute to the Element

func (*Element) SrcSet added in v0.16.9

func (element *Element) SrcSet(value string) *Element

SrcSet adds a "srcset" attribute to the Element

func (*Element) Start added in v0.9.0

func (element *Element) Start() *Element

Start writes the initial tag name and opening bracket for this tag.

func (*Element) Style added in v0.11.2

func (element *Element) Style(values ...string) *Element

Style adds a "style" attribute to the Element Multiple style definitions can be passed, and are separated by semicolons.

func (*Element) TabIndex added in v0.11.4

func (element *Element) TabIndex(value string) *Element

TabIndex adds a "tabIndex" attribute to the Element

func (*Element) Type added in v0.9.1

func (element *Element) Type(value string) *Element

Type adds a "type" attribute to the Element

func (*Element) Value added in v0.9.0

func (element *Element) Value(value string) *Element

Value adds a "value" attribute to the Element. NOTE: This attributed is ALWAYS written to the HTML tag, even if it is empty.

Jump to

Keyboard shortcuts

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