html

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-html

Go Report Card Documentation license GitHub version GitHub issues

This is a Golang library which allows you to build HTML using Go code.

It's mainly designed as a way to create re-usable HTML components, similar to how React components work.

Usage

Let's start with an easy example. This is the generated HTML:

<div class="userProfile">
  <h1 class="profileName">
    pclaerhout
  </h1>
  <img src="https://en.gravatar.com/userimage/389631/32413201cb4a680e82ee7c4bdd30b795.jpeg" class="profileImage" alt="pclaerhout">
  </img>
</div>

This is the Go code which generates this HTML:

package components

import (
	"github.com/pieterclaerhout/go-html"
)

type UserProfile struct {
	Username  string
	AvatarURL string
}

// HTML returns the HTML version of the component
func (userProfile *UserProfile) HTML(minified bool) string {

	root := html.Blocks{
		html.Div(
			html.Class("userProfile"),
			html.H1(
				html.Class("profileName"),
				html.Text(userProfile.Username),
			),
			html.Img(
				html.Class("profileImage").Src(userProfile.AvatarURL).Alt(userProfile.Username),
			),
		),
	}

	return html.Render(root, minified)

}

Let's assume you want to create this blob of HTML:

<div class="page-header">
  <table>
    <tr>
      <td>
        <img src="https://en.gravatar.com/userimage/389631/32413201cb4a680e82ee7c4bdd30b795.jpeg" alt="Title" class="app-icon-left shadow">
        </img>
      </td>
      <td>
        <h1>
          Title
        </h1> d
        <h3>
          Subtitle
        </h3>
      </td>
    </tr>
  </table>
</div>

To express the following HTML code in Go code using this library, you write:

package components

import (
	"github.com/pieterclaerhout/go-html"
)

type PageHeader struct {
	Title    string
	Subtitle string
	IconURL  string
}

// HTML returns the HTML version of the component
func (pageHeader *PageHeader) HTML(minified bool) string {

	root := html.Blocks{
		html.Div(
			html.Class("page-header"),
			html.Table(nil,
				html.Tr(nil,
					html.Td(nil,
						html.Img(html.Src(pageHeader.IconURL).Class("app-icon-left shadow").Alt(pageHeader.Title)),
					),
					html.Td(nil,
						html.H1(nil, html.Text(pageHeader.Title)),
						html.H3(nil, html.Text(pageHeader.Subtitle)),
					),
				),
			),
		),
	}

	return html.Render(root, minified)

}

You can also make it slightly more complex, e.g. to build a HTML select box:

<select>
  <option value="1">label 1</option>
  <option value="2" selected="true">label 2</option>
  <option value="3">label 3</option>
</select>

The equivalent Go code:

package components

import (
	"github.com/pieterclaerhout/go-html"
)

type Option struct {
	Value string
	Text  string
}

type MySelectBuilder struct {
	options  []Option
	selected string
}

func MySelect() *MySelectBuilder {
	return &MySelectBuilder{}
}

func (b *MySelectBuilder) Options(opts []Option) (r *MySelectBuilder) {
	b.options = opts
	return b
}

func (b *MySelectBuilder) Selected(selected string) (r *MySelectBuilder) {
	b.selected = selected
	return b
}

// HTML returns the HTML version of the component
func (b *MySelectBuilder) HTML(minified bool) string {

	options := []html.Block{}
	for _, op := range b.options {

		attrs := html.Value(op.Value)
		if op.Value == b.selected {
			attrs = attrs.Attr("selected", "true")
		}

		options = append(options, html.Option(attrs, html.Text(op.Text)))

	}

	root := html.Blocks{
		html.Select(nil, options...),
	}

	return html.Render(root, minified)

}

Credits

This library is a fork of https://github.com/mbertschler/blocks with additional features and incompatible changes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(blocks Block, minified bool) string

func RenderMinified

func RenderMinified(root Block, w io.Writer) error

func RenderMinifiedString

func RenderMinifiedString(root Block) (string, error)

func RenderString

func RenderString(root Block) (string, error)

func RenderToWriter

func RenderToWriter(w io.Writer, root Block) error

Types

type AttrPair

type AttrPair struct {
	Key   string
	Value interface{}
}

AttrPair defines a n attribute key and value pair

type Attributes

type Attributes []AttrPair

Attributes defines a list of attribute pairs

func Action

func Action(action interface{}) Attributes

func Alt

func Alt(name interface{}) Attributes

func Attr

func Attr(key string, value interface{}) Attributes

Attr returns an attribute pair with the given key and value

func Charset

func Charset(charset interface{}) Attributes

func Checked

func Checked() Attributes

func Class

func Class(class interface{}) Attributes

func Content

func Content(name interface{}) Attributes

func Data

func Data(key string, value interface{}) Attributes

func Defer

func Defer() Attributes

func For

func For(fo interface{}) Attributes

func Href

func Href(href interface{}) Attributes

func ID

func ID(id interface{}) Attributes

func Method

func Method(method interface{}) Attributes

func Name

func Name(name interface{}) Attributes

func Rel

func Rel(rel interface{}) Attributes

func Src

func Src(src interface{}) Attributes

func Styles

func Styles(style string) Attributes

func Type

func Type(typ interface{}) Attributes

func Value

func Value(value interface{}) Attributes

func (Attributes) Action

func (a Attributes) Action(action interface{}) Attributes

func (Attributes) Alt

func (a Attributes) Alt(name interface{}) Attributes

func (Attributes) Attr

func (a Attributes) Attr(key string, id interface{}) Attributes

Attr adds an attribute to the list of attribute pairs

func (Attributes) Charset

func (a Attributes) Charset(charset interface{}) Attributes

func (Attributes) Checked

func (a Attributes) Checked() Attributes

func (Attributes) Class

func (a Attributes) Class(class interface{}) Attributes

func (Attributes) Content

func (a Attributes) Content(name interface{}) Attributes

func (Attributes) Data

func (a Attributes) Data(key string, value interface{}) Attributes

func (Attributes) Defer

func (a Attributes) Defer() Attributes

func (Attributes) For

func (a Attributes) For(fo interface{}) Attributes

func (Attributes) Href

func (a Attributes) Href(href interface{}) Attributes

func (Attributes) ID

func (a Attributes) ID(id interface{}) Attributes

func (Attributes) Method

func (a Attributes) Method(method interface{}) Attributes

func (Attributes) Name

func (a Attributes) Name(name interface{}) Attributes

func (Attributes) Rel

func (a Attributes) Rel(rel interface{}) Attributes

func (Attributes) Src

func (a Attributes) Src(src interface{}) Attributes

func (Attributes) Styles

func (a Attributes) Styles(style string) Attributes

func (Attributes) Type

func (a Attributes) Type(typ interface{}) Attributes

func (Attributes) Value

func (a Attributes) Value(value interface{}) Attributes

type Block

type Block interface {
	RenderHTML() Block
}

func A

func A(attr Attributes, children ...Block) Block

func Abbr

func Abbr(attr Attributes, children ...Block) Block

func Acronym

func Acronym(attr Attributes, children ...Block) Block

func Address

func Address(attr Attributes, children ...Block) Block

func Area

func Area(attr Attributes) Block

func Article

func Article(attr Attributes, children ...Block) Block

func Aside

func Aside(attr Attributes, children ...Block) Block

func Audio

func Audio(attr Attributes, children ...Block) Block

func B

func B(attr Attributes, children ...Block) Block

func Base

func Base(attr Attributes) Block

func Bdi

func Bdi(attr Attributes, children ...Block) Block

func Bdo

func Bdo(attr Attributes, children ...Block) Block

func Big

func Big(attr Attributes, children ...Block) Block

func Blockquote

func Blockquote(attr Attributes, children ...Block) Block

func Body

func Body(attr Attributes, children ...Block) Block

func Br

func Br() Block

func Button

func Button(attr Attributes, children ...Block) Block

func Canvas

func Canvas(attr Attributes, children ...Block) Block

func Caption

func Caption(attr Attributes, children ...Block) Block

func Cite

func Cite(attr Attributes, children ...Block) Block

func Col

func Col(attr Attributes) Block

func Colgroup

func Colgroup(attr Attributes, children ...Block) Block

func Datalist

func Datalist(attr Attributes, children ...Block) Block

func Dd

func Dd(attr Attributes, children ...Block) Block

func Del

func Del(attr Attributes, children ...Block) Block

func Details

func Details(attr Attributes, children ...Block) Block

func Dfn

func Dfn(attr Attributes, children ...Block) Block

func Dialog

func Dialog(attr Attributes, children ...Block) Block

func Dir

func Dir(attr Attributes, children ...Block) Block

func Div

func Div(attr Attributes, children ...Block) Block

func Dl

func Dl(attr Attributes, children ...Block) Block

func Doctype

func Doctype(arg string) Block

func Dt

func Dt(attr Attributes, children ...Block) Block

func Elem

func Elem(el string, attr Attributes, children ...Block) Block

Elem returns a new ad-hoc element with the given name, attributes and children

func Em

func Em(attr Attributes, children ...Block) Block

func Embed

func Embed(attr Attributes) Block

func Fieldset

func Fieldset(attr Attributes, children ...Block) Block

func Figcaption

func Figcaption(attr Attributes, children ...Block) Block

func Figure

func Figure(attr Attributes, children ...Block) Block
func Footer(attr Attributes, children ...Block) Block

func Form

func Form(attr Attributes, children ...Block) Block

func Frameset

func Frameset(attr Attributes, children ...Block) Block

func H1

func H1(attr Attributes, children ...Block) Block

func H2

func H2(attr Attributes, children ...Block) Block

func H3

func H3(attr Attributes, children ...Block) Block

func H4

func H4(attr Attributes, children ...Block) Block

func H5

func H5(attr Attributes, children ...Block) Block

func H6

func H6(attr Attributes, children ...Block) Block
func Head(attr Attributes, children ...Block) Block
func Header(attr Attributes, children ...Block) Block

func Hr

func Hr(attr Attributes) Block

func Html

func Html(attr Attributes, children ...Block) Block

func I

func I(attr Attributes, children ...Block) Block

func Iframe

func Iframe(attr Attributes, children ...Block) Block

func Img

func Img(attr Attributes) Block

func Input

func Input(attr Attributes) Block

func Ins

func Ins(attr Attributes, children ...Block) Block

func Kbd

func Kbd(attr Attributes, children ...Block) Block

func Label

func Label(attr Attributes, children ...Block) Block

func Legend

func Legend(attr Attributes, children ...Block) Block

func Li

func Li(attr Attributes, children ...Block) Block
func Link(attr Attributes) Block

func Main

func Main(attr Attributes, children ...Block) Block

func Map

func Map(attr Attributes, children ...Block) Block

func Mark

func Mark(attr Attributes, children ...Block) Block

func Meta

func Meta(attr Attributes, children ...Block) Block

func Meter

func Meter(attr Attributes, children ...Block) Block
func Nav(attr Attributes, children ...Block) Block

func Noframes

func Noframes(attr Attributes, children ...Block) Block

func Noscript

func Noscript(attr Attributes, children ...Block) Block

func Object

func Object(attr Attributes) Block

func Ol

func Ol(attr Attributes, children ...Block) Block

func Optgroup

func Optgroup(attr Attributes, children ...Block) Block

func Option

func Option(attr Attributes, children ...Block) Block

func Output

func Output(attr Attributes) Block

func P

func P(attr Attributes, children ...Block) Block

func Param

func Param(attr Attributes) Block

func Picture

func Picture(attr Attributes, children ...Block) Block

func Pre

func Pre(attr Attributes, children ...Block) Block

func Progress

func Progress(attr Attributes) Block

func Q

func Q(attr Attributes, children ...Block) Block

func Rp

func Rp(attr Attributes, children ...Block) Block

func Rt

func Rt(attr Attributes, children ...Block) Block

func Ruby

func Ruby(attr Attributes, children ...Block) Block

func S

func S(attr Attributes, children ...Block) Block

func Samp

func Samp(attr Attributes, children ...Block) Block

func Script

func Script(attr Attributes, children ...Block) Block

func Section

func Section(attr Attributes, children ...Block) Block

func Select

func Select(attr Attributes, children ...Block) Block

func Small

func Small(attr Attributes, children ...Block) Block

func Source

func Source(attr Attributes) Block

func Span

func Span(attr Attributes, children ...Block) Block

func Strike

func Strike(attr Attributes, children ...Block) Block

func Strong

func Strong(attr Attributes, children ...Block) Block

func Style

func Style(attr Attributes, children ...Block) Block

func Sub

func Sub(attr Attributes, children ...Block) Block

func Summary

func Summary(attr Attributes, children ...Block) Block

func Sup

func Sup(attr Attributes, children ...Block) Block

func Svg

func Svg(attr Attributes, children ...Block) Block

func Table

func Table(attr Attributes, children ...Block) Block

func Tbody

func Tbody(attr Attributes, children ...Block) Block

func Td

func Td(attr Attributes, children ...Block) Block

func Template

func Template(attr Attributes, children ...Block) Block

func Textarea

func Textarea(attr Attributes, children ...Block) Block

func Tfoot

func Tfoot(attr Attributes, children ...Block) Block

func Th

func Th(attr Attributes, children ...Block) Block

func Thead

func Thead(attr Attributes, children ...Block) Block

func Time

func Time(attr Attributes, children ...Block) Block

func Title

func Title(attr Attributes, children ...Block) Block

func Tr

func Tr(attr Attributes, children ...Block) Block

func Track

func Track(attr Attributes) Block

func U

func U(attr Attributes, children ...Block) Block

func Ul

func Ul(attr Attributes, children ...Block) Block

func Var

func Var(attr Attributes, children ...Block) Block

func Video

func Video(attr Attributes, children ...Block) Block

func Wbr

func Wbr(attr Attributes, children ...Block) Block

type Blocks

type Blocks []Block

Blocks defines a list of HTML blocks

func (*Blocks) Add

func (b *Blocks) Add(block Block)

Add adds a block to the list

func (*Blocks) AddBlocks

func (b *Blocks) AddBlocks(blocks Blocks)

AddBlocks adds multiple blocks to the list

func (Blocks) RenderHTML

func (Blocks) RenderHTML() Block

RenderHTML renders the block as HTML

type CSS

type CSS template.CSS

func (CSS) RenderHTML

func (CSS) RenderHTML() Block

type Comment

type Comment string

func (Comment) RenderHTML

func (Comment) RenderHTML() Block

type Element

type Element struct {
	Type       string        // The type of the element
	Attributes               // The attributes for the element
	Children   Blocks        // The children for the element
	Options    ElementOption // The options for the element
}

Element defines a HTML element

func (Element) RenderHTML

func (Element) RenderHTML() Block

RenderHTML renders the element as HTML

type ElementOption

type ElementOption int8

ElementOption defines the options for the elements

const (
	// Void is the default (no options)
	Void ElementOption = 1 << iota

	// SelfClose defines the element as a self-closing element
	SelfClose

	// CSSElement defines the element as CSS element
	CSSElement

	// JSElement defines the element as a JS element
	JSElement

	// NoWhitespace defines that the element shouldn't add whitespace
	NoWhitespace
)

type HTML

type HTML template.HTML

func (HTML) RenderHTML

func (HTML) RenderHTML() Block

type HTMLAttr

type HTMLAttr template.HTMLAttr

func (HTMLAttr) RenderHTML

func (HTMLAttr) RenderHTML() Block

type JS

type JS template.JS

func (JS) RenderHTML

func (JS) RenderHTML() Block

type JSStr

type JSStr template.JSStr

func (JSStr) RenderHTML

func (JSStr) RenderHTML() Block

type Text

type Text string

func (Text) RenderHTML

func (Text) RenderHTML() Block

type URL

type URL template.URL

func (URL) RenderHTML

func (URL) RenderHTML() Block

type UnsafeString

type UnsafeString string

func (UnsafeString) RenderHTML

func (UnsafeString) RenderHTML() Block

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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