html

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: Apache-2.0 Imports: 3 Imported by: 7

README

html 🚧

GoDoc Go Report Card Build Status Codecov Version

Efficient HTML Tag Assembly

This is a simple library for assembling HTML tags using Go.

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. Builder uses an efficient strings.Builder](https://pkg.go.dev/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) BR added in v0.9.0

func (b *Builder) BR() *Element

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) 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) Input added in v0.9.0

func (b *Builder) Input() *Element

func (*Builder) Label added in v0.9.0

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

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

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) Attr added in v0.9.0

func (element *Element) Attr(name string, value interface{}) *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.

func (*Element) Class added in v0.9.0

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

Class adds a "class" attribute to the Element

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

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