html

package module
v0.16.11 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: Apache-2.0 Imports: 3 Imported by: 7

README

html 🚧

GoDoc Version Build Status Go Report Card Codecov

Efficient HTML Tag Assembly

This library builds a string buffer of HTML tags programmatically.

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

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

func (*Builder) Audio added in v0.16.5

func (b *Builder) Audio() *Element

func (*Builder) B added in v0.11.2

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

func (*Builder) BR added in v0.9.0

func (b *Builder) BR() *Element

func (*Builder) Body added in v0.16.4

func (b *Builder) Body() *Element

func (*Builder) Button added in v0.10.0

func (b *Builder) Button() *Element

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

func (*Builder) Div added in v0.9.0

func (b *Builder) Div() *Element

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

func (*Builder) Figure added in v0.16.7

func (b *Builder) Figure() *Element

func (*Builder) Form added in v0.11.0

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

func (*Builder) H1 added in v0.11.2

func (b *Builder) H1() *Element

func (*Builder) H2 added in v0.11.2

func (b *Builder) H2() *Element

func (*Builder) H3 added in v0.11.2

func (b *Builder) H3() *Element

func (*Builder) HTML added in v0.16.4

func (b *Builder) HTML() *Element

func (*Builder) Head added in v0.16.4

func (b *Builder) Head() *Element

func (*Builder) I added in v0.11.2

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

func (*Builder) Img added in v0.16.5

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

func (*Builder) Input added in v0.9.0

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

func (*Builder) Label added in v0.9.0

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

func (*Builder) OptGroup added in v0.10.0

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

func (*Builder) Option added in v0.10.0

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

func (*Builder) OptionSelected added in v0.16.6

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

func (*Builder) Picture added in v0.16.7

func (b *Builder) Picture() *Element

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

func (*Builder) Select added in v0.10.0

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

func (*Builder) Source added in v0.16.5

func (b *Builder) Source() *Element

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

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

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

func (*Builder) TD added in v0.11.2

func (b *Builder) TD() *Element

func (*Builder) TH added in v0.11.2

func (b *Builder) TH() *Element

func (*Builder) TR added in v0.11.2

func (b *Builder) TR() *Element

func (*Builder) Table added in v0.11.2

func (b *Builder) Table() *Element

func (*Builder) Textarea added in v0.10.0

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

func (*Builder) Title added in v0.16.4

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

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) 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

Stype 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

Jump to

Keyboard shortcuts

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