htmlgo

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: MIT Imports: 8 Imported by: 48

README

htmlgo

import "github.com/go-rvq/htmlgo"

## htmlgo

Type safe and modularize way to generate html on server side. Download the package with `go get -v github.com/go-rvq/htmlgo` and import the package with `.` gives you simpler code:

import (
	. "github.com/go-rvq/htmlgo"
)

also checkout full API documentation at: https://godoc.org/github.com/go-rvq/htmlgo

Index

Variables

var InlineTags = map[string]bool{
    "a":        true,
    "abbr":     true,
    "acronym":  true,
    "b":        true,
    "bdo":      true,
    "big":      true,
    "br":       true,
    "button":   true,
    "cite":     true,
    "code":     true,
    "dfn":      true,
    "em":       true,
    "i":        true,
    "img":      true,
    "input":    true,
    "kbd":      true,
    "label":    true,
    "map":      true,
    "object":   true,
    "output":   true,
    "q":        true,
    "samp":     true,
    "select":   true,
    "small":    true,
    "span":     true,
    "strong":   true,
    "sub":      true,
    "sup":      true,
    "textarea": true,
    "time":     true,
    "tt":       true,
    "var":      true,
}

func Enter

func Enter(w io.Writer, f func(ctx *Context) error) (err error)

func EscapeAttr

func EscapeAttr(str string) (r string)

func Fprint

func Fprint(w io.Writer, root HTMLComponent, ctx context.Context) (err error)

func IsInline

func IsInline(c HTMLComponent) bool

func IsRaw

func IsRaw(c HTMLComponent) (ok bool)

func JSONString

func JSONString(v any) (r string)

func Marshal

func Marshal(comp HTMLComponent, ctx context.Context) (_ []byte, err error)

func MarshallString

func MarshallString(c HTMLComponent, ctx context.Context) (string, error)

func MustString

func MustString(root HTMLComponent, ctx context.Context) string

func NewTag

func NewTag[T TagBuilderGetter[T]](dot T, name string, children ...HTMLComponent) T

func Simplify

func Simplify(c HTMLComponent, cb func(HTMLComponent))

Simplify Simplifies components walking over nested HTMLComponents and calls cb if component not is nil

func SimplifyE

func SimplifyE(c HTMLComponent, cb func(HTMLComponent) (err error)) (err error)

func Write

func Write(w io.Writer, comp ...HTMLComponent) (err error)

type Attr

type Attr struct {
    Key   string
    Value any
}

func (*Attr) Override
func (a *Attr) Override(f func(old any) any)

type Attrs

type Attrs []*Attr

func (Attrs) Get
func (a Attrs) Get(name string) *Attr

func (*Attrs) Remove
func (a *Attrs) Remove(name string)

func (*Attrs) RemoveMany
func (a *Attrs) RemoveMany(name ...string)

type ComponentFunc

type ComponentFunc func(ctx *Context) (err error)
Example

Use ComponentFunc with RawHTML and Component

userProfile := func(username string, avatarURL string) HTMLComponent {
	return ComponentFunc(func(ctx *Context) (err error) {
		return Div(
			H1(username).Class("profileName"),
			P(Img(avatarURL).Class("profileImage")),
			Svg(RawHTML("the svg")),
		).Class("userProfile").Write(ctx)
	})
}

comp := Ul(
	Li(
		userProfile("felix<h1>", "http://image.com/img1.png"),
	),
	Li(
		userProfile("john", "http://image.com/img2.png"),
	),
)
Fprint(os.Stdout, comp, context.TODO())
// Output:
// <ul>
//	<li>
//		<div class='userProfile'>
//			<h1 class='profileName'>felix&lt;h1&gt;</h1>
//			<p><img src='http://image.com/img1.png' class='profileImage'></p>
//			<Svg>the svg</Svg>
//		</div>
//	</li>
//	<li>
//		<div class='userProfile'>
//			<h1 class='profileName'>john</h1>
//			<p><img src='http://image.com/img2.png' class='profileImage'></p>
//			<Svg>the svg</Svg>
//		</div>
//	</li>
// </ul>
Output
<ul>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>felix&lt;h1&gt;</h1>
			<p><img src='http://image.com/img1.png' class='profileImage'></p>
			<Svg>the svg</Svg>
		</div>
	</li>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>john</h1>
			<p><img src='http://image.com/img2.png' class='profileImage'></p>
			<Svg>the svg</Svg>
		</div>
	</li>
</ul>

Example (With Raw HTML)

Use ComponentFunc with RawHTML and Component

userProfile := func(username string, avatarURL string) HTMLComponent {
	return ComponentFunc(func(ctx *Context) (err error) {
		return Div(
			H1(username).Class("profileName"),
			Img(avatarURL).Class("profileImage"),
			P(RawHTML("<svg>complicated svg</svg>")),
			P(RawHTML("<custom-tag>custom 1</custom-tag>"), Span("text"), B("bolded")),
			P(RawHTML("<custom-tag>custom 2</custom-tag>\n\t\t\t\t"), Span("text 2"), B("bolded 2"), RawHTML("<custom-tag>custom 3</custom-tag>")),
			Div(RawHTML("end")),
		).Class("userProfile").Write(ctx)
	})
}

comp := Ul(
	Li(
		userProfile("felix<h1>", "http://image.com/img1.png"),
	),
	Li(
		userProfile("john", "http://image.com/img2.png"),
	),
)

Fprint(os.Stdout, comp, context.TODO())
// Output:
// <ul>
//	<li>
//		<div class='userProfile'>
//			<h1 class='profileName'>felix&lt;h1&gt;</h1>
//			<img src='http://image.com/img1.png' class='profileImage'>
//			<p><svg>complicated svg</svg></p>
//			<p><custom-tag>custom 1</custom-tag><span>text</span><b>bolded</b></p>
//			<p><custom-tag>custom 2</custom-tag>
//				<span>text 2</span><b>bolded 2</b><custom-tag>custom 3</custom-tag></p>
//			<div>end</div>
//		</div>
//	</li>
//	<li>
//		<div class='userProfile'>
//			<h1 class='profileName'>john</h1>
//			<img src='http://image.com/img2.png' class='profileImage'>
//			<p><svg>complicated svg</svg></p>
//			<p><custom-tag>custom 1</custom-tag><span>text</span><b>bolded</b></p>
//			<p><custom-tag>custom 2</custom-tag>
//				<span>text 2</span><b>bolded 2</b><custom-tag>custom 3</custom-tag></p>
//			<div>end</div>
//		</div>
//	</li>
// </ul>
Output
<ul>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>felix&lt;h1&gt;</h1>
			<img src='http://image.com/img1.png' class='profileImage'>
			<p><svg>complicated svg</svg></p>
			<p><custom-tag>custom 1</custom-tag><span>text</span><b>bolded</b></p>
			<p><custom-tag>custom 2</custom-tag>
				<span>text 2</span><b>bolded 2</b><custom-tag>custom 3</custom-tag></p>
			<div>end</div>
		</div>
	</li>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>john</h1>
			<img src='http://image.com/img2.png' class='profileImage'>
			<p><svg>complicated svg</svg></p>
			<p><custom-tag>custom 1</custom-tag><span>text</span><b>bolded</b></p>
			<p><custom-tag>custom 2</custom-tag>
				<span>text 2</span><b>bolded 2</b><custom-tag>custom 3</custom-tag></p>
			<div>end</div>
		</div>
	</li>
</ul>

func (ComponentFunc) Write
func (f ComponentFunc) Write(ctx *Context) error

type ContainerHTMLComponent

type ContainerHTMLComponent interface {
    HTMLComponent
    HasChilds() bool
    Append(...HTMLComponent)
    Prepend(...HTMLComponent)
    GetChildren() HTMLComponents
    SetChildren(HTMLComponents)
}

type Context

type Context struct {
    Writer
    Context context.Context
    // contains filtered or unexported fields
}

func NewContext
func NewContext(w Writer, ctx context.Context) *Context

func (*Context) Depth
func (ctx *Context) Depth() int

func (*Context) Enter
func (ctx *Context) Enter(f func() (err error)) error

func (*Context) EnterLeave
func (ctx *Context) EnterLeave() func()

func (*Context) Leave
func (ctx *Context) Leave()

func (*Context) LeftSpace
func (ctx *Context) LeftSpace() string

func (*Context) Value
func (ctx *Context) Value(name any) any

func (*Context) Write
func (ctx *Context) Write(comp HTMLComponent) (err error)

func (*Context) WriteChildren
func (ctx *Context) WriteChildren(l HTMLComponents) (err error)

func (*Context) WriteLeftSpace
func (ctx *Context) WriteLeftSpace() error

type HTMLComponent

type HTMLComponent interface {
    Write(w *Context) (err error)
}

func HTML
func HTML(children ...HTMLComponent) (r HTMLComponent)

HTML "html": HTMLHtmlElement;

Example

Create a full html page

comp := HTML(
	Head(
		Meta().Charset("utf8"),
		Title("My test page"),
	),
	Body(
		Div(
			Img("images/firefox-icon.png").Alt("My test image"),
		),
	),
)
Fprint(os.Stdout, comp, context.TODO())
// Output:
// <!DOCTYPE html>
// <html>
//	<head>
//		<meta charset='utf8'>
//		<title>My test page</title>
//	</head>
//	<body>
//		<div><img src='images/firefox-icon.png' alt='My test image'></div>
//	</body>
// </html>
Output
<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf8'>
		<title>My test page</title>
	</head>
	<body>
		<div><img src='images/firefox-icon.png' alt='My test image'></div>
	</body>
</html>

Example (Http Handler)

An example about how to integrate into http.Handler, and how to do layout, and how to use context.

type User struct {
	Name string
}
type key string

const currentUserKey key = "currentUser"

userStatus := func() HTMLComponent {
	return ComponentFunc(func(ctx *Context) (err error) {
		if currentUser, ok := ctx.Value(currentUserKey).(*User); ok {
			return Div(
				Text(currentUser.Name),
			).Class("username").Write(ctx)
		}
		return Div(Text("Login")).Class("login").Write(ctx)
	})
}

myHeader := func() HTMLComponent {
	return Div(
		Text("header"),
		userStatus(),
	).Class("header")
}
myFooter := func() HTMLComponent {
	return Div(Text("footer")).Class("footer")
}

layout := func(in HTMLComponent) (out HTMLComponent) {
	out = HTML(
		Head(
			Meta().Charset("utf8"),
		),
		Body(
			myHeader(),
			in,
			myFooter(),
		),
	)
	return
}

getLoginUserFromCookie := func(r *http.Request) *User {
	return &User{Name: "felix"}
}

homeHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	user := getLoginUserFromCookie(r)
	ctx := context.WithValue(context.TODO(), currentUserKey, user)

	root := Div(
		Text("This is my home page"),
	)

	Fprint(w, layout(root), ctx)
})

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
homeHandler.ServeHTTP(w, r)

fmt.Println(w.Body.String())

// Output:
// <!DOCTYPE html>
// <html>
//	<head>
//		<meta charset='utf8'>
//	</head>
//	<body>
//		<div class='header'>header
//			<div class='username'>felix</div>
//		</div>
//		<div>This is my home page</div>
//		<div class='footer'>footer</div>
//	</body>
// </html>
Output
<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf8'>
	</head>
	<body>
		<div class='header'>header
			<div class='username'>felix</div>
		</div>
		<div>This is my home page</div>
		<div class='footer'>footer</div>
	</body>
</html>

func SimplifyComponent
func SimplifyComponent(c HTMLComponent) HTMLComponent

func Text
func Text(text string) HTMLComponent

func Textf
func Textf(format string, a ...any) HTMLComponent

type HTMLComponents

type HTMLComponents []HTMLComponent

func Components
func Components(comps ...HTMLComponent) HTMLComponents

func SimplifyItems
func SimplifyItems(c HTMLComponent) (r HTMLComponents)

func (HTMLComponents) Write
func (l HTMLComponents) Write(ctx *Context) (err error)

type HTMLTagBuilder

type HTMLTagBuilder struct {
    TagName      string
    IsOmitEndTag bool
    Attrs        Attrs
    Styles       []string
    ClassNames   []string
    Childs       HTMLComponents
    IsInLine     bool
}

func A
func A(children ...HTMLComponent) (r *HTMLTagBuilder)

A "a": HTMLAnchorElement;

func Abbr
func Abbr(text string) (r *HTMLTagBuilder)

Abbr "abbr": HTMLElement;

func Address
func Address(children ...HTMLComponent) (r *HTMLTagBuilder)

Address "address": HTMLElement;

func Area
func Area() (r *HTMLTagBuilder)

Area "area": HTMLAreaElement;

func Article
func Article(children ...HTMLComponent) (r *HTMLTagBuilder)

Article "article": HTMLElement;

func Aside
func Aside(children ...HTMLComponent) (r *HTMLTagBuilder)

Aside "aside": HTMLElement;

func Audio
func Audio(children ...HTMLComponent) (r *HTMLTagBuilder)

Audio "audio": HTMLAudioElement;

func B
func B(text string) (r *HTMLTagBuilder)

B "b": HTMLElement;

func Base
func Base() (r *HTMLTagBuilder)

Base "base": HTMLBaseElement;

func Bdi
func Bdi(text string) (r *HTMLTagBuilder)

Bdi "bdi": HTMLElement;

func Bdo
func Bdo(text string) (r *HTMLTagBuilder)

Bdo "bdo": HTMLElement;

func Blockquote
func Blockquote(children ...HTMLComponent) (r *HTMLTagBuilder)

Blockquote "blockquote": HTMLQuoteElement;

func Body
func Body(children ...HTMLComponent) (r *HTMLTagBuilder)

Body "body": HTMLBodyElement;

func Br
func Br() (r *HTMLTagBuilder)

Br "br": HTMLBRElement;

func Button
func Button(label string) (r *HTMLTagBuilder)

Button "button": HTMLButtonElement;

func Canvas
func Canvas(children ...HTMLComponent) (r *HTMLTagBuilder)

Canvas "canvas": HTMLCanvasElement;

func Caption
func Caption(text string) (r *HTMLTagBuilder)

Caption "caption": HTMLTableCaptionElement;

func Cite
func Cite(children ...HTMLComponent) (r *HTMLTagBuilder)

Cite "cite": HTMLElement;

func Code
func Code(text string) (r *HTMLTagBuilder)

Code "code": HTMLElement;

func Col
func Col() (r *HTMLTagBuilder)

Col "col": HTMLTableColElement;

func Colgroup
func Colgroup(children ...HTMLComponent) (r *HTMLTagBuilder)

Colgroup "colgroup": HTMLTableColElement;

func Data
func Data(children ...HTMLComponent) (r *HTMLTagBuilder)

Data "data": HTMLDataElement;

func Datalist
func Datalist(children ...HTMLComponent) (r *HTMLTagBuilder)

Datalist "datalist": HTMLDataListElement;

func Dd
func Dd(children ...HTMLComponent) (r *HTMLTagBuilder)

Dd "dd": HTMLElement;

func Del
func Del(text string) (r *HTMLTagBuilder)

Del "del": HTMLModElement;

func Details
func Details(children ...HTMLComponent) (r *HTMLTagBuilder)

func Dfn
func Dfn(text string) (r *HTMLTagBuilder)

Dfn "dfn": HTMLElement;

func Dialog
func Dialog(children ...HTMLComponent) (r *HTMLTagBuilder)

Dialog "dialog": HTMLDialogElement;

func Div
func Div(children ...HTMLComponent) (r *HTMLTagBuilder)

Div "div": HTMLDivElement;

func Dl
func Dl(children ...HTMLComponent) (r *HTMLTagBuilder)

Dl "dl": HTMLDListElement;

func Dt
func Dt(children ...HTMLComponent) (r *HTMLTagBuilder)

Dt "dt": HTMLElement;

func Em
func Em(text string) (r *HTMLTagBuilder)

Em "em": HTMLElement;

func Embed
func Embed() (r *HTMLTagBuilder)

Embed "embed": HTMLEmbedElement;

func Fieldset
func Fieldset(children ...HTMLComponent) (r *HTMLTagBuilder)

Fieldset "fieldset": HTMLFieldSetElement;

func Figcaption
func Figcaption(text string) (r *HTMLTagBuilder)

Figcaption "figcaption": HTMLElement;

func Figure
func Figure(children ...HTMLComponent) (r *HTMLTagBuilder)

Figure "figure": HTMLElement;

func Footer(children ...HTMLComponent) (r *HTMLTagBuilder)

Footer "footer": HTMLElement;

func Form
func Form(children ...HTMLComponent) (r *HTMLTagBuilder)

Form "form": HTMLFormElement;

func H1
func H1(text string) (r *HTMLTagBuilder)

H1 "h1": HTMLHeadingElement;

func H2
func H2(text string) (r *HTMLTagBuilder)

H2 "h2": HTMLHeadingElement;

func H3
func H3(text string) (r *HTMLTagBuilder)

H3 "h3": HTMLHeadingElement;

func H4
func H4(text string) (r *HTMLTagBuilder)

H4 "h4": HTMLHeadingElement;

func H5
func H5(text string) (r *HTMLTagBuilder)

H5 "h5": HTMLHeadingElement;

func H6
func H6(text string) (r *HTMLTagBuilder)

H6 "h6": HTMLHeadingElement;

func Head
func Head(children ...HTMLComponent) (r *HTMLTagBuilder)

func Header
func Header(children ...HTMLComponent) (r *HTMLTagBuilder)

Header "header": HTMLElement;

func Hgroup
func Hgroup(children ...HTMLComponent) (r *HTMLTagBuilder)

Hgroup "hgroup": HTMLElement;

func Hr
func Hr() (r *HTMLTagBuilder)

Hr "hr": HTMLHRElement;

func I
func I(text string) (r *HTMLTagBuilder)

I "i": HTMLElement;

func Iframe
func Iframe(children ...HTMLComponent) (r *HTMLTagBuilder)

Iframe "iframe": HTMLIFrameElement;

func Img
func Img(src string) (r *HTMLTagBuilder)

Img "img": HTMLImageElement;

func Input
func Input(name string) (r *HTMLTagBuilder)

func Ins
func Ins(children ...HTMLComponent) (r *HTMLTagBuilder)

Ins "ins": HTMLModElement;

func Kbd
func Kbd(text string) (r *HTMLTagBuilder)

Kbd "kbd": HTMLElement;

func Label
func Label(text string) (r *HTMLTagBuilder)

Label "label": HTMLLabelElement;

func Legend
func Legend(text string) (r *HTMLTagBuilder)

Legend "legend": HTMLLegendElement;

func Li
func Li(children ...HTMLComponent) (r *HTMLTagBuilder)

Li "li": HTMLLIElement;

func Link(href string) (r *HTMLTagBuilder)

Link "link": HTMLLinkElement;

func Main
func Main(children ...HTMLComponent) (r *HTMLTagBuilder)

Main "main": HTMLElement;

func Map
func Map(children ...HTMLComponent) (r *HTMLTagBuilder)

Map "map": HTMLMapElement;

func Mark
func Mark(text string) (r *HTMLTagBuilder)

Mark "mark": HTMLElement;

func Menu
func Menu(children ...HTMLComponent) (r *HTMLTagBuilder)

Menu "menu": HTMLMenuElement;

func Meta
func Meta() (r *HTMLTagBuilder)

func Meter
func Meter(children ...HTMLComponent) (r *HTMLTagBuilder)

Meter "meter": HTMLMeterElement;

func Nav
func Nav(children ...HTMLComponent) (r *HTMLTagBuilder)

Nav "nav": HTMLElement;

func Noscript
func Noscript(children ...HTMLComponent) (r *HTMLTagBuilder)

Noscript "noscript": HTMLElement;

func Object
func Object(data string) (r *HTMLTagBuilder)

Object "object": HTMLObjectElement;

func Ol
func Ol(children ...HTMLComponent) (r *HTMLTagBuilder)

Ol "ol": HTMLOListElement;

func Optgroup
func Optgroup(children ...HTMLComponent) (r *HTMLTagBuilder)

Optgroup "optgroup": HTMLOptGroupElement;

func Option
func Option(text string) (r *HTMLTagBuilder)

Option "option": HTMLOptionElement;

func Output
func Output(children ...HTMLComponent) (r *HTMLTagBuilder)

Output "output": HTMLOutputElement;

func P
func P(children ...HTMLComponent) (r *HTMLTagBuilder)

P "p": HTMLParagraphElement;

func Param
func Param(name string) (r *HTMLTagBuilder)

Param "param": HTMLParamElement;

func Picture
func Picture(children ...HTMLComponent) (r *HTMLTagBuilder)

Picture "picture": HTMLPictureElement;

func Pre
func Pre(text string) (r *HTMLTagBuilder)

Pre "pre": HTMLPreElement;

func Progress
func Progress(children ...HTMLComponent) (r *HTMLTagBuilder)

Progress "progress": HTMLProgressElement;

func Q
func Q(text string) (r *HTMLTagBuilder)

Q "q": HTMLQuoteElement;

func Rp
func Rp(text string) (r *HTMLTagBuilder)

Rp "rp": HTMLElement;

func Rt
func Rt(text string) (r *HTMLTagBuilder)

Rt "rt": HTMLElement;

func Ruby
func Ruby(children ...HTMLComponent) (r *HTMLTagBuilder)

Ruby "ruby": HTMLElement;

func S
func S(text string) (r *HTMLTagBuilder)

S "s": HTMLElement;

func Samp
func Samp(children ...HTMLComponent) (r *HTMLTagBuilder)

func Script
func Script(script string) (r *HTMLTagBuilder)

Script "script": HTMLScriptElement;

Example

Write a little bit of JavaScript and stylesheet

comp := Div(
	Button("Hello").ID("hello"),
	Style(`
	.container {
		background-color: red;
	}
`),

	Script(`
	var b = document.getElementById("hello")
	b.onclick = function(e){
		alert("Hello");
	}
`),
).Class("container")

Fprint(os.Stdout, comp, context.TODO())
// Output:
// <div class='container'><button id='hello'>Hello</button>
//	<style type='text/css'>
//		.container {
//			background-color: red;
//		}
//	</style>
//	<script type='text/javascript'>
//		var b = document.getElementById("hello")
//		b.onclick = function(e){
//			alert("Hello");
//		}
//	</script>
// </div>
Output
<div class='container'><button id='hello'>Hello</button>
	<style type='text/css'>
		.container {
			background-color: red;
		}
	</style>
	<script type='text/javascript'>
		var b = document.getElementById("hello")
		b.onclick = function(e){
			alert("Hello");
		}
	</script>
</div>

func Section
func Section(children ...HTMLComponent) (r *HTMLTagBuilder)

Section "section": HTMLElement;

func Select
func Select(children ...HTMLComponent) (r *HTMLTagBuilder)

Select "select": HTMLSelectElement;

func Slot
func Slot(children ...HTMLComponent) (r *HTMLTagBuilder)

Slot "slot": HTMLSlotElement;

func Small
func Small(text string) (r *HTMLTagBuilder)

Small "small": HTMLElement;

func Source
func Source(src string) (r *HTMLTagBuilder)

Source "source": HTMLSourceElement;

func Span
func Span(text string) (r *HTMLTagBuilder)

Span "span": HTMLSpanElement;

func Strong
func Strong(text string) (r *HTMLTagBuilder)

Strong "strong": HTMLElement;

func Style
func Style(style string) (r *HTMLTagBuilder)

Style "style": HTMLStyleElement;

func Sub
func Sub(text string) (r *HTMLTagBuilder)

Sub "sub": HTMLElement;

func Summary
func Summary(children ...HTMLComponent) (r *HTMLTagBuilder)

Summary "summary": HTMLElement;

func Sup
func Sup(text string) (r *HTMLTagBuilder)

Sup "sup": HTMLElement;

func Svg
func Svg(children ...HTMLComponent) (r *HTMLTagBuilder)

Svg "svg": HTMLElement;

func Table
func Table(children ...HTMLComponent) (r *HTMLTagBuilder)

Table "table": HTMLTableElement;

func Tag
func Tag(tag string, child ...HTMLComponent) *HTMLTagBuilder
Example

Create a simple div, Text will be escaped by html

banner := "We write html in Go"
comp := Div(
	Text("123<h1>"),
	Textf("Hello, %s", banner),
	Br(),
	Text("new line"),
)
Fprint(os.Stdout, comp, context.TODO())
// Output:
// <div>123&lt;h1&gt;Hello, We write html in Go<br>new line</div>
Output
<div>123&lt;h1&gt;Hello, We write html in Go<br>new line</div>

Example (Iff)

An example to use If, `Iff` is for body to passed in as an func for the body depends on if condition not to be nil, `If` is for directly passed in HTMLComponent

type Person struct {
	Age int
}
var p *Person

name := "Leon"
comp := Div(
	Iff(p != nil && p.Age > 18, func() HTMLComponent {
		return Div().Text(name + ": Age > 18")
	}).ElseIf(p == nil, func() HTMLComponent {
		return Div().Text("No person named " + name)
	}).Else(func() HTMLComponent {
		return Div().Text(name + ":Age <= 18")
	}),
)
Fprint(os.Stdout, comp, context.TODO())
// Output:
// <div>
//	<div>No person named Leon</div>
// </div>
Output
<div>
	<div>No person named Leon</div>
</div>

Example (Styles)

An example show how to set styles

comp := Div().
	StyleIf("background-color:red; border:1px solid red;", true).
	StyleIf("color:blue", true)
Fprint(os.Stdout, comp, context.TODO())
// Output:
// <div style='background-color:red; border:1px solid red; color:blue;'></div>
Output
<div style='background-color:red; border:1px solid red; color:blue;'></div>

Example (With Mutiple Type Attrs)

An example show how to set different type of attributes

type MoreData struct {
	Name  string
	Count int
}
comp := Div().
	Attr("true-attr", true).
	Attr("false-attr", false).
	Attr("int-attr", 1).
	Attr("float-attr", 123.456).
	Attr("str-attr", "the string value").
	Attr("safe-str-value", SafeAttr(`{the shafe value}`)).
	Attr("byte-attr", []byte("byte value")).
	Attr("rune-attr", []rune("rune value")).
	Attr("struct-attr", &MoreData{Name: "felix", Count: 100})
Fprint(os.Stdout, comp, context.TODO())
// Output:
// <div true-attr int-attr='1' float-attr='123.456000' str-attr='the string value' safe-str-value={the shafe value} byte-attr='byte value' rune-attr='rune value' struct-attr='{"Name":"felix","Count":100}'></div>
Output
<div true-attr int-attr='1' float-attr='123.456000' str-attr='the string value' safe-str-value={the shafe value} byte-attr='byte value' rune-attr='rune value' struct-attr='{"Name":"felix","Count":100}'></div>

func Tbody
func Tbody(children ...HTMLComponent) (r *HTMLTagBuilder)

Tbody "tbody": HTMLTableSectionElement;

func Td
func Td(children ...HTMLComponent) (r *HTMLTagBuilder)

Td "td": HTMLTableDataCellElement;

func Template
func Template(children ...HTMLComponent) (r *HTMLTagBuilder)

Template "template": HTMLTemplateElement;

func Textarea
func Textarea(text string) (r *HTMLTagBuilder)

Textarea "textarea": HTMLTextAreaElement;

func Tfoot
func Tfoot(children ...HTMLComponent) (r *HTMLTagBuilder)

Tfoot "tfoot": HTMLTableSectionElement;

func Th
func Th(text string) (r *HTMLTagBuilder)

Th "th": HTMLTableHeaderCellElement;

func Thead
func Thead(children ...HTMLComponent) (r *HTMLTagBuilder)

Thead "thead": HTMLTableSectionElement;

func Time
func Time(datetime string) (r *HTMLTagBuilder)

Time "time": HTMLTimeElement;

func Title
func Title(text string) (r *HTMLTagBuilder)

Title "title": HTMLTitleElement;

func Tr
func Tr(children ...HTMLComponent) (r *HTMLTagBuilder)

Tr "tr": HTMLTableRowElement;

func Track
func Track(src string) (r *HTMLTagBuilder)

Track "track": HTMLTrackElement;

func U
func U(text string) (r *HTMLTagBuilder)

U "u": HTMLElement;

func Ul
func Ul(children ...HTMLComponent) (r *HTMLTagBuilder)

Ul "ul": HTMLUListElement;

func Var
func Var(text string) (r *HTMLTagBuilder)

Var "var": HTMLElement;

func Video
func Video(children ...HTMLComponent) (r *HTMLTagBuilder)

Video "video": HTMLVideoElement;

func Wbr
func Wbr() (r *HTMLTagBuilder)

Wbr "wbr": HTMLElement;

func (*HTMLTagBuilder) Action
func (b *HTMLTagBuilder) Action(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Alt
func (b *HTMLTagBuilder) Alt(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Append
func (b *HTMLTagBuilder) Append(comp ...HTMLComponent)

func (*HTMLTagBuilder) AppendChildren
func (b *HTMLTagBuilder) AppendChildren(c ...HTMLComponent) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Attr
func (b *HTMLTagBuilder) Attr(vs ...any) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) AttrIf
func (b *HTMLTagBuilder) AttrIf(key, value any, add bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Charset
func (b *HTMLTagBuilder) Charset(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Checked
func (b *HTMLTagBuilder) Checked(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Children
func (b *HTMLTagBuilder) Children(comps ...HTMLComponent) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Class
func (b *HTMLTagBuilder) Class(names ...string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) ClassIf
func (b *HTMLTagBuilder) ClassIf(name string, add bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Content
func (b *HTMLTagBuilder) Content(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Data
func (b *HTMLTagBuilder) Data(vs ...string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Disabled
func (b *HTMLTagBuilder) Disabled(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) For
func (b *HTMLTagBuilder) For(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) GetAttr
func (b *HTMLTagBuilder) GetAttr(key string) *Attr

func (*HTMLTagBuilder) GetChildren
func (b *HTMLTagBuilder) GetChildren() HTMLComponents

func (*HTMLTagBuilder) HasChilds
func (b *HTMLTagBuilder) HasChilds() bool

func (*HTMLTagBuilder) Href
func (b *HTMLTagBuilder) Href(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) ID
func (b *HTMLTagBuilder) ID(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Inline
func (b *HTMLTagBuilder) Inline() (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Method
func (b *HTMLTagBuilder) Method(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Name
func (b *HTMLTagBuilder) Name(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) OmitEndTag
func (b *HTMLTagBuilder) OmitEndTag() (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Placeholder
func (b *HTMLTagBuilder) Placeholder(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Prepend
func (b *HTMLTagBuilder) Prepend(comp ...HTMLComponent)

func (*HTMLTagBuilder) PrependChildren
func (b *HTMLTagBuilder) PrependChildren(c ...HTMLComponent) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Property
func (b *HTMLTagBuilder) Property(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Readonly
func (b *HTMLTagBuilder) Readonly(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Rel
func (b *HTMLTagBuilder) Rel(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) RemoveAttr
func (b *HTMLTagBuilder) RemoveAttr(key ...string) *HTMLTagBuilder

func (*HTMLTagBuilder) Required
func (b *HTMLTagBuilder) Required(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Role
func (b *HTMLTagBuilder) Role(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) SetAttr
func (b *HTMLTagBuilder) SetAttr(k string, v any)

func (*HTMLTagBuilder) SetChildren
func (b *HTMLTagBuilder) SetChildren(comps HTMLComponents)

func (*HTMLTagBuilder) Src
func (b *HTMLTagBuilder) Src(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) String
func (b *HTMLTagBuilder) String() string

func (*HTMLTagBuilder) Style
func (b *HTMLTagBuilder) Style(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) StyleIf
func (b *HTMLTagBuilder) StyleIf(v string, add bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) TabIndex
func (b *HTMLTagBuilder) TabIndex(v int) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Tag
func (b *HTMLTagBuilder) Tag(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Target
func (b *HTMLTagBuilder) Target(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Text
func (b *HTMLTagBuilder) Text(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Title
func (b *HTMLTagBuilder) Title(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Type
func (b *HTMLTagBuilder) Type(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Value
func (b *HTMLTagBuilder) Value(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Write
func (b *HTMLTagBuilder) Write(ctx *Context) (err error)

type IfBuilder

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

func If
func If(v bool, comps ...HTMLComponent) (r *IfBuilder)

func (*IfBuilder) Else
func (b *IfBuilder) Else(comps ...HTMLComponent) (r *IfBuilder)

func (*IfBuilder) ElseIf
func (b *IfBuilder) ElseIf(v bool, comps ...HTMLComponent) (r *IfBuilder)

func (*IfBuilder) Write
func (b *IfBuilder) Write(ctx *Context) (_ error)

type IfFuncBuilder

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

func Iff
func Iff(v bool, f func() HTMLComponent) (r *IfFuncBuilder)

func (*IfFuncBuilder) Else
func (b *IfFuncBuilder) Else(f func() HTMLComponent) (r *IfFuncBuilder)

func (*IfFuncBuilder) ElseIf
func (b *IfFuncBuilder) ElseIf(v bool, f func() HTMLComponent) (r *IfFuncBuilder)

func (*IfFuncBuilder) Write
func (b *IfFuncBuilder) Write(ctx *Context) (_ error)

type MutableAttrHTMLComponent

type MutableAttrHTMLComponent interface {
    HTMLComponent
    SetAttr(k string, v any)
}

type RawHTML

type RawHTML string

func (RawHTML) Write
func (s RawHTML) Write(ctx *Context) (err error)

type SafeAttr

SafeAttr is a special attr value for unescaped attr value

type SafeAttr string

type TagBuilder

type TagBuilder[T any] struct {
    DOT     T
    HTMLTag *HTMLTagBuilder
}

func (*TagBuilder[T]) Action
func (t *TagBuilder[T]) Action(v string) T

func (*TagBuilder[T]) Alt
func (t *TagBuilder[T]) Alt(v string) T

func (*TagBuilder[T]) AppendChild
func (t *TagBuilder[T]) AppendChild(c ...HTMLComponent) T

func (*TagBuilder[T]) Attr
func (t *TagBuilder[T]) Attr(vs ...any) T

func (*TagBuilder[T]) AttrIf
func (t *TagBuilder[T]) AttrIf(key, value any, add bool) T

func (*TagBuilder[T]) Charset
func (t *TagBuilder[T]) Charset(v string) T

func (*TagBuilder[T]) Checked
func (t *TagBuilder[T]) Checked(v bool) T

func (*TagBuilder[T]) Children
func (t *TagBuilder[T]) Children(c ...HTMLComponent) T

func (*TagBuilder[T]) Class
func (t *TagBuilder[T]) Class(names ...string) T

func (*TagBuilder[T]) ClassIf
func (t *TagBuilder[T]) ClassIf(name string, add bool) T

func (*TagBuilder[T]) Content
func (t *TagBuilder[T]) Content(v string) T

func (*TagBuilder[T]) Data
func (t *TagBuilder[T]) Data(vs ...string) T

func (*TagBuilder[T]) Disabled
func (t *TagBuilder[T]) Disabled(v bool) T

func (*TagBuilder[T]) Dot
func (t *TagBuilder[T]) Dot() T

func (*TagBuilder[T]) For
func (t *TagBuilder[T]) For(v string) T

func (*TagBuilder[T]) GetAttr
func (t *TagBuilder[T]) GetAttr(key string) *Attr

func (*TagBuilder[T]) GetChildren
func (t *TagBuilder[T]) GetChildren() []HTMLComponent

func (*TagBuilder[T]) GetHTMLTagBuilder
func (t *TagBuilder[T]) GetHTMLTagBuilder() *HTMLTagBuilder

func (*TagBuilder[T]) GetTagBuilder
func (t *TagBuilder[T]) GetTagBuilder() *TagBuilder[T]

func (*TagBuilder[T]) Href
func (t *TagBuilder[T]) Href(v string) T

func (*TagBuilder[T]) ID
func (t *TagBuilder[T]) ID(v string) T

func (*TagBuilder[T]) Method
func (t *TagBuilder[T]) Method(v string) T

func (*TagBuilder[T]) Name
func (t *TagBuilder[T]) Name(v string) T

func (*TagBuilder[T]) OmitEndTag
func (t *TagBuilder[T]) OmitEndTag() T

func (*TagBuilder[T]) Placeholder
func (t *TagBuilder[T]) Placeholder(v string) T

func (*TagBuilder[T]) PrependChild
func (t *TagBuilder[T]) PrependChild(c ...HTMLComponent) T

func (*TagBuilder[T]) Property
func (t *TagBuilder[T]) Property(v string) T

func (*TagBuilder[T]) Readonly
func (t *TagBuilder[T]) Readonly(v bool) T

func (*TagBuilder[T]) Rel
func (t *TagBuilder[T]) Rel(v string) T

func (*TagBuilder[T]) RemoveAttr
func (t *TagBuilder[T]) RemoveAttr(key ...string) T

func (*TagBuilder[T]) Required
func (t *TagBuilder[T]) Required(v bool) T

func (*TagBuilder[T]) Role
func (t *TagBuilder[T]) Role(v string) T

func (*TagBuilder[T]) SetAttr
func (t *TagBuilder[T]) SetAttr(k string, v any) T

func (*TagBuilder[T]) SetTag
func (t *TagBuilder[T]) SetTag(v string) T

func (*TagBuilder[T]) Src
func (t *TagBuilder[T]) Src(v string) T

func (*TagBuilder[T]) String
func (t *TagBuilder[T]) String() string

func (*TagBuilder[T]) Style
func (t *TagBuilder[T]) Style(v string) T

func (*TagBuilder[T]) StyleIf
func (t *TagBuilder[T]) StyleIf(v string, add bool) T

func (*TagBuilder[T]) TabIndex
func (t *TagBuilder[T]) TabIndex(v int) T

func (*TagBuilder[T]) TagName
func (t *TagBuilder[T]) TagName() string

func (*TagBuilder[T]) Target
func (t *TagBuilder[T]) Target(v string) T

func (*TagBuilder[T]) Text
func (t *TagBuilder[T]) Text(v string) T

func (*TagBuilder[T]) Title
func (t *TagBuilder[T]) Title(v string) T

func (*TagBuilder[T]) Type
func (t *TagBuilder[T]) Type(v string) T

func (*TagBuilder[T]) Value
func (t *TagBuilder[T]) Value(v string) T

func (*TagBuilder[T]) Write
func (t *TagBuilder[T]) Write(ctx *Context) error

type TagBuilderGetter

type TagBuilderGetter[T any] interface {
    TagGetter
    GetTagBuilder() *TagBuilder[T]
}

type TagGetter

type TagGetter interface {
    GetHTMLTagBuilder() *HTMLTagBuilder
}

type Writer

type Writer interface {
    WriteString(s string) (err error)
    WriteByte(b byte) (err error)
    Write(b []byte) (err error)
}

func ToWriter
func ToWriter(w io.Writer) Writer

type WriterImpl

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

func (*WriterImpl) Write
func (w *WriterImpl) Write(b []byte) (err error)

func (*WriterImpl) WriteByte
func (w *WriterImpl) WriteByte(b byte) (err error)

func (*WriterImpl) WriteString
func (w *WriterImpl) WriteString(s string) (err error)

func (*WriterImpl) Writer
func (w *WriterImpl) Writer() io.Writer

Generated by gomarkdoc

Documentation

Overview

## htmlgo

Type safe and modularize way to generate html on server side. Download the package with `go get -v github.com/go-rvq/htmlgo` and import the package with `.` gives you simpler code:

import (
	. "github.com/go-rvq/htmlgo"
)

also checkout full API documentation at: https://godoc.org/github.com/go-rvq/htmlgo

Index

Examples

Constants

This section is empty.

Variables

View Source
var InlineTags = map[string]bool{
	"a":        true,
	"abbr":     true,
	"acronym":  true,
	"b":        true,
	"bdo":      true,
	"big":      true,
	"br":       true,
	"button":   true,
	"cite":     true,
	"code":     true,
	"dfn":      true,
	"em":       true,
	"i":        true,
	"img":      true,
	"input":    true,
	"kbd":      true,
	"label":    true,
	"map":      true,
	"object":   true,
	"output":   true,
	"q":        true,
	"samp":     true,
	"select":   true,
	"small":    true,
	"span":     true,
	"strong":   true,
	"sub":      true,
	"sup":      true,
	"textarea": true,
	"time":     true,
	"tt":       true,
	"var":      true,
}

Functions

func Enter

func Enter(w io.Writer, f func(ctx *Context) error) (err error)

func EscapeAttr

func EscapeAttr(str string) (r string)

func Fprint

func Fprint(w io.Writer, root HTMLComponent, ctx context.Context) (err error)

func IsInline

func IsInline(c HTMLComponent) bool

func IsRaw

func IsRaw(c HTMLComponent) (ok bool)

func JSONString

func JSONString(v any) (r string)

func Marshal added in v1.0.2

func Marshal(comp HTMLComponent, ctx context.Context) (_ []byte, err error)

func MarshallString

func MarshallString(c HTMLComponent, ctx context.Context) (string, error)

func MustString

func MustString(root HTMLComponent, ctx context.Context) string

func NewTag

func NewTag[T TagBuilderGetter[T]](dot T, name string, children ...HTMLComponent) T

func Simplify

func Simplify(c HTMLComponent, cb func(HTMLComponent))

Simplify Simplifies components walking over nested HTMLComponents and calls cb if component not is nil

func SimplifyE

func SimplifyE(c HTMLComponent, cb func(HTMLComponent) (err error)) (err error)

func Write

func Write(w io.Writer, comp ...HTMLComponent) (err error)

Types

type Attr

type Attr struct {
	Key   string
	Value any
}

func (*Attr) Override

func (a *Attr) Override(f func(old any) any)

type Attrs

type Attrs []*Attr

func (Attrs) Get

func (a Attrs) Get(name string) *Attr

func (*Attrs) Remove

func (a *Attrs) Remove(name string)

func (*Attrs) RemoveMany

func (a *Attrs) RemoveMany(name ...string)

type ComponentFunc

type ComponentFunc func(ctx *Context) (err error)
Example

Use ComponentFunc with RawHTML and Component

userProfile := func(username string, avatarURL string) HTMLComponent {
	return ComponentFunc(func(ctx *Context) (err error) {
		return Div(
			H1(username).Class("profileName"),
			P(Img(avatarURL).Class("profileImage")),
			Svg(RawHTML("the svg")),
		).Class("userProfile").Write(ctx)
	})
}

comp := Ul(
	Li(
		userProfile("felix<h1>", "http://image.com/img1.png"),
	),
	Li(
		userProfile("john", "http://image.com/img2.png"),
	),
)
Fprint(os.Stdout, comp, context.TODO())
Output:

<ul>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>felix&lt;h1&gt;</h1>
			<p><img src='http://image.com/img1.png' class='profileImage'></p>
			<Svg>the svg</Svg>
		</div>
	</li>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>john</h1>
			<p><img src='http://image.com/img2.png' class='profileImage'></p>
			<Svg>the svg</Svg>
		</div>
	</li>
</ul>
Example (WithRawHTML)

Use ComponentFunc with RawHTML and Component

userProfile := func(username string, avatarURL string) HTMLComponent {
	return ComponentFunc(func(ctx *Context) (err error) {
		return Div(
			H1(username).Class("profileName"),
			Img(avatarURL).Class("profileImage"),
			P(RawHTML("<svg>complicated svg</svg>")),
			P(RawHTML("<custom-tag>custom 1</custom-tag>"), Span("text"), B("bolded")),
			P(RawHTML("<custom-tag>custom 2</custom-tag>\n\t\t\t\t"), Span("text 2"), B("bolded 2"), RawHTML("<custom-tag>custom 3</custom-tag>")),
			Div(RawHTML("end")),
		).Class("userProfile").Write(ctx)
	})
}

comp := Ul(
	Li(
		userProfile("felix<h1>", "http://image.com/img1.png"),
	),
	Li(
		userProfile("john", "http://image.com/img2.png"),
	),
)

Fprint(os.Stdout, comp, context.TODO())
Output:

<ul>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>felix&lt;h1&gt;</h1>
			<img src='http://image.com/img1.png' class='profileImage'>
			<p><svg>complicated svg</svg></p>
			<p><custom-tag>custom 1</custom-tag><span>text</span><b>bolded</b></p>
			<p><custom-tag>custom 2</custom-tag>
				<span>text 2</span><b>bolded 2</b><custom-tag>custom 3</custom-tag></p>
			<div>end</div>
		</div>
	</li>
	<li>
		<div class='userProfile'>
			<h1 class='profileName'>john</h1>
			<img src='http://image.com/img2.png' class='profileImage'>
			<p><svg>complicated svg</svg></p>
			<p><custom-tag>custom 1</custom-tag><span>text</span><b>bolded</b></p>
			<p><custom-tag>custom 2</custom-tag>
				<span>text 2</span><b>bolded 2</b><custom-tag>custom 3</custom-tag></p>
			<div>end</div>
		</div>
	</li>
</ul>

func (ComponentFunc) Write

func (f ComponentFunc) Write(ctx *Context) error

type ContainerHTMLComponent

type ContainerHTMLComponent interface {
	HTMLComponent
	HasChilds() bool
	Append(...HTMLComponent)
	Prepend(...HTMLComponent)
	GetChildren() HTMLComponents
	SetChildren(HTMLComponents)
}

type Context

type Context struct {
	Writer
	Context context.Context
	// contains filtered or unexported fields
}

func NewContext

func NewContext(w Writer, ctx context.Context) *Context

func (*Context) Depth

func (ctx *Context) Depth() int

func (*Context) Enter

func (ctx *Context) Enter(f func() (err error)) error

func (*Context) EnterLeave

func (ctx *Context) EnterLeave() func()

func (*Context) Leave

func (ctx *Context) Leave()

func (*Context) LeftSpace

func (ctx *Context) LeftSpace() string

func (*Context) Value

func (ctx *Context) Value(name any) any

func (*Context) Write

func (ctx *Context) Write(comp HTMLComponent) (err error)

func (*Context) WriteChildren

func (ctx *Context) WriteChildren(l HTMLComponents) (err error)

func (*Context) WriteLeftSpace

func (ctx *Context) WriteLeftSpace() error

type HTMLComponent

type HTMLComponent interface {
	Write(w *Context) (err error)
}

func HTML

func HTML(children ...HTMLComponent) (r HTMLComponent)

HTML "html": HTMLHtmlElement;

Example

Create a full html page

comp := HTML(
	Head(
		Meta().Charset("utf8"),
		Title("My test page"),
	),
	Body(
		Div(
			Img("images/firefox-icon.png").Alt("My test image"),
		),
	),
)
Fprint(os.Stdout, comp, context.TODO())
Output:

<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf8'>
		<title>My test page</title>
	</head>
	<body>
		<div><img src='images/firefox-icon.png' alt='My test image'></div>
	</body>
</html>
Example (HttpHandler)

An example about how to integrate into http.Handler, and how to do layout, and how to use context.

type User struct {
	Name string
}
type key string

const currentUserKey key = "currentUser"

userStatus := func() HTMLComponent {
	return ComponentFunc(func(ctx *Context) (err error) {
		if currentUser, ok := ctx.Value(currentUserKey).(*User); ok {
			return Div(
				Text(currentUser.Name),
			).Class("username").Write(ctx)
		}
		return Div(Text("Login")).Class("login").Write(ctx)
	})
}

myHeader := func() HTMLComponent {
	return Div(
		Text("header"),
		userStatus(),
	).Class("header")
}
myFooter := func() HTMLComponent {
	return Div(Text("footer")).Class("footer")
}

layout := func(in HTMLComponent) (out HTMLComponent) {
	out = HTML(
		Head(
			Meta().Charset("utf8"),
		),
		Body(
			myHeader(),
			in,
			myFooter(),
		),
	)
	return
}

getLoginUserFromCookie := func(r *http.Request) *User {
	return &User{Name: "felix"}
}

homeHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	user := getLoginUserFromCookie(r)
	ctx := context.WithValue(context.TODO(), currentUserKey, user)

	root := Div(
		Text("This is my home page"),
	)

	Fprint(w, layout(root), ctx)
})

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
homeHandler.ServeHTTP(w, r)

fmt.Println(w.Body.String())
Output:

<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf8'>
	</head>
	<body>
		<div class='header'>header
			<div class='username'>felix</div>
		</div>
		<div>This is my home page</div>
		<div class='footer'>footer</div>
	</body>
</html>

func SimplifyComponent

func SimplifyComponent(c HTMLComponent) HTMLComponent

func Text

func Text(text string) HTMLComponent

func Textf

func Textf(format string, a ...any) HTMLComponent

type HTMLComponents

type HTMLComponents []HTMLComponent

func Components

func Components(comps ...HTMLComponent) HTMLComponents

func SimplifyItems

func SimplifyItems(c HTMLComponent) (r HTMLComponents)

func (HTMLComponents) Write

func (l HTMLComponents) Write(ctx *Context) (err error)

type HTMLTagBuilder

type HTMLTagBuilder struct {
	TagName      string
	IsOmitEndTag bool
	Attrs        Attrs
	Styles       []string
	ClassNames   []string
	Childs       HTMLComponents
	IsInLine     bool
}

func A

func A(children ...HTMLComponent) (r *HTMLTagBuilder)

A "a": HTMLAnchorElement;

func Abbr

func Abbr(text string) (r *HTMLTagBuilder)

Abbr "abbr": HTMLElement;

func Address

func Address(children ...HTMLComponent) (r *HTMLTagBuilder)

Address "address": HTMLElement;

func Area

func Area() (r *HTMLTagBuilder)

Area "area": HTMLAreaElement;

func Article

func Article(children ...HTMLComponent) (r *HTMLTagBuilder)

Article "article": HTMLElement;

func Aside

func Aside(children ...HTMLComponent) (r *HTMLTagBuilder)

Aside "aside": HTMLElement;

func Audio

func Audio(children ...HTMLComponent) (r *HTMLTagBuilder)

Audio "audio": HTMLAudioElement;

func B

func B(text string) (r *HTMLTagBuilder)

B "b": HTMLElement;

func Base

func Base() (r *HTMLTagBuilder)

Base "base": HTMLBaseElement;

func Bdi

func Bdi(text string) (r *HTMLTagBuilder)

Bdi "bdi": HTMLElement;

func Bdo

func Bdo(text string) (r *HTMLTagBuilder)

Bdo "bdo": HTMLElement;

func Blockquote

func Blockquote(children ...HTMLComponent) (r *HTMLTagBuilder)

Blockquote "blockquote": HTMLQuoteElement;

func Body

func Body(children ...HTMLComponent) (r *HTMLTagBuilder)

Body "body": HTMLBodyElement;

func Br

func Br() (r *HTMLTagBuilder)

Br "br": HTMLBRElement;

func Button

func Button(label string) (r *HTMLTagBuilder)

Button "button": HTMLButtonElement;

func Canvas

func Canvas(children ...HTMLComponent) (r *HTMLTagBuilder)

Canvas "canvas": HTMLCanvasElement;

func Caption

func Caption(text string) (r *HTMLTagBuilder)

Caption "caption": HTMLTableCaptionElement;

func Cite

func Cite(children ...HTMLComponent) (r *HTMLTagBuilder)

Cite "cite": HTMLElement;

func Code

func Code(text string) (r *HTMLTagBuilder)

Code "code": HTMLElement;

func Col

func Col() (r *HTMLTagBuilder)

Col "col": HTMLTableColElement;

func Colgroup

func Colgroup(children ...HTMLComponent) (r *HTMLTagBuilder)

Colgroup "colgroup": HTMLTableColElement;

func Data

func Data(children ...HTMLComponent) (r *HTMLTagBuilder)

Data "data": HTMLDataElement;

func Datalist

func Datalist(children ...HTMLComponent) (r *HTMLTagBuilder)

Datalist "datalist": HTMLDataListElement;

func Dd

func Dd(children ...HTMLComponent) (r *HTMLTagBuilder)

Dd "dd": HTMLElement;

func Del

func Del(text string) (r *HTMLTagBuilder)

Del "del": HTMLModElement;

func Details

func Details(children ...HTMLComponent) (r *HTMLTagBuilder)

func Dfn

func Dfn(text string) (r *HTMLTagBuilder)

Dfn "dfn": HTMLElement;

func Dialog

func Dialog(children ...HTMLComponent) (r *HTMLTagBuilder)

Dialog "dialog": HTMLDialogElement;

func Div

func Div(children ...HTMLComponent) (r *HTMLTagBuilder)

Div "div": HTMLDivElement;

func Dl

func Dl(children ...HTMLComponent) (r *HTMLTagBuilder)

Dl "dl": HTMLDListElement;

func Dt

func Dt(children ...HTMLComponent) (r *HTMLTagBuilder)

Dt "dt": HTMLElement;

func Em

func Em(text string) (r *HTMLTagBuilder)

Em "em": HTMLElement;

func Embed

func Embed() (r *HTMLTagBuilder)

Embed "embed": HTMLEmbedElement;

func Fieldset

func Fieldset(children ...HTMLComponent) (r *HTMLTagBuilder)

Fieldset "fieldset": HTMLFieldSetElement;

func Figcaption

func Figcaption(text string) (r *HTMLTagBuilder)

Figcaption "figcaption": HTMLElement;

func Figure

func Figure(children ...HTMLComponent) (r *HTMLTagBuilder)

Figure "figure": HTMLElement;

func Footer(children ...HTMLComponent) (r *HTMLTagBuilder)

Footer "footer": HTMLElement;

func Form

func Form(children ...HTMLComponent) (r *HTMLTagBuilder)

Form "form": HTMLFormElement;

func H1

func H1(text string) (r *HTMLTagBuilder)

H1 "h1": HTMLHeadingElement;

func H2

func H2(text string) (r *HTMLTagBuilder)

H2 "h2": HTMLHeadingElement;

func H3

func H3(text string) (r *HTMLTagBuilder)

H3 "h3": HTMLHeadingElement;

func H4

func H4(text string) (r *HTMLTagBuilder)

H4 "h4": HTMLHeadingElement;

func H5

func H5(text string) (r *HTMLTagBuilder)

H5 "h5": HTMLHeadingElement;

func H6

func H6(text string) (r *HTMLTagBuilder)

H6 "h6": HTMLHeadingElement;

func Head(children ...HTMLComponent) (r *HTMLTagBuilder)
func Header(children ...HTMLComponent) (r *HTMLTagBuilder)

Header "header": HTMLElement;

func Hgroup

func Hgroup(children ...HTMLComponent) (r *HTMLTagBuilder)

Hgroup "hgroup": HTMLElement;

func Hr

func Hr() (r *HTMLTagBuilder)

Hr "hr": HTMLHRElement;

func I

func I(text string) (r *HTMLTagBuilder)

I "i": HTMLElement;

func Iframe

func Iframe(children ...HTMLComponent) (r *HTMLTagBuilder)

Iframe "iframe": HTMLIFrameElement;

func Img

func Img(src string) (r *HTMLTagBuilder)

Img "img": HTMLImageElement;

func Input

func Input(name string) (r *HTMLTagBuilder)

func Ins

func Ins(children ...HTMLComponent) (r *HTMLTagBuilder)

Ins "ins": HTMLModElement;

func Kbd

func Kbd(text string) (r *HTMLTagBuilder)

Kbd "kbd": HTMLElement;

func Label

func Label(text string) (r *HTMLTagBuilder)

Label "label": HTMLLabelElement;

func Legend

func Legend(text string) (r *HTMLTagBuilder)

Legend "legend": HTMLLegendElement;

func Li

func Li(children ...HTMLComponent) (r *HTMLTagBuilder)

Li "li": HTMLLIElement;

func Link(href string) (r *HTMLTagBuilder)

Link "link": HTMLLinkElement;

func Main

func Main(children ...HTMLComponent) (r *HTMLTagBuilder)

Main "main": HTMLElement;

func Map

func Map(children ...HTMLComponent) (r *HTMLTagBuilder)

Map "map": HTMLMapElement;

func Mark

func Mark(text string) (r *HTMLTagBuilder)

Mark "mark": HTMLElement;

func Menu(children ...HTMLComponent) (r *HTMLTagBuilder)

Menu "menu": HTMLMenuElement;

func Meta

func Meta() (r *HTMLTagBuilder)

func Meter

func Meter(children ...HTMLComponent) (r *HTMLTagBuilder)

Meter "meter": HTMLMeterElement;

func Nav(children ...HTMLComponent) (r *HTMLTagBuilder)

Nav "nav": HTMLElement;

func Noscript

func Noscript(children ...HTMLComponent) (r *HTMLTagBuilder)

Noscript "noscript": HTMLElement;

func Object

func Object(data string) (r *HTMLTagBuilder)

Object "object": HTMLObjectElement;

func Ol

func Ol(children ...HTMLComponent) (r *HTMLTagBuilder)

Ol "ol": HTMLOListElement;

func Optgroup

func Optgroup(children ...HTMLComponent) (r *HTMLTagBuilder)

Optgroup "optgroup": HTMLOptGroupElement;

func Option

func Option(text string) (r *HTMLTagBuilder)

Option "option": HTMLOptionElement;

func Output

func Output(children ...HTMLComponent) (r *HTMLTagBuilder)

Output "output": HTMLOutputElement;

func P

func P(children ...HTMLComponent) (r *HTMLTagBuilder)

P "p": HTMLParagraphElement;

func Param

func Param(name string) (r *HTMLTagBuilder)

Param "param": HTMLParamElement;

func Picture

func Picture(children ...HTMLComponent) (r *HTMLTagBuilder)

Picture "picture": HTMLPictureElement;

func Pre

func Pre(text string) (r *HTMLTagBuilder)

Pre "pre": HTMLPreElement;

func Progress

func Progress(children ...HTMLComponent) (r *HTMLTagBuilder)

Progress "progress": HTMLProgressElement;

func Q

func Q(text string) (r *HTMLTagBuilder)

Q "q": HTMLQuoteElement;

func Rp

func Rp(text string) (r *HTMLTagBuilder)

Rp "rp": HTMLElement;

func Rt

func Rt(text string) (r *HTMLTagBuilder)

Rt "rt": HTMLElement;

func Ruby

func Ruby(children ...HTMLComponent) (r *HTMLTagBuilder)

Ruby "ruby": HTMLElement;

func S

func S(text string) (r *HTMLTagBuilder)

S "s": HTMLElement;

func Samp

func Samp(children ...HTMLComponent) (r *HTMLTagBuilder)

func Script

func Script(script string) (r *HTMLTagBuilder)

Script "script": HTMLScriptElement;

Example

Write a little bit of JavaScript and stylesheet

comp := Div(
	Button("Hello").ID("hello"),
	Style(`
		.container {
			background-color: red;
		}
	`),

	Script(`
		var b = document.getElementById("hello")
		b.onclick = function(e){
			alert("Hello");
		}
	`),
).Class("container")

Fprint(os.Stdout, comp, context.TODO())
Output:

<div class='container'><button id='hello'>Hello</button>
	<style type='text/css'>
		.container {
			background-color: red;
		}
	</style>
	<script type='text/javascript'>
		var b = document.getElementById("hello")
		b.onclick = function(e){
			alert("Hello");
		}
	</script>
</div>

func Section

func Section(children ...HTMLComponent) (r *HTMLTagBuilder)

Section "section": HTMLElement;

func Select

func Select(children ...HTMLComponent) (r *HTMLTagBuilder)

Select "select": HTMLSelectElement;

func Slot

func Slot(children ...HTMLComponent) (r *HTMLTagBuilder)

Slot "slot": HTMLSlotElement;

func Small

func Small(text string) (r *HTMLTagBuilder)

Small "small": HTMLElement;

func Source

func Source(src string) (r *HTMLTagBuilder)

Source "source": HTMLSourceElement;

func Span

func Span(text string) (r *HTMLTagBuilder)

Span "span": HTMLSpanElement;

func Strong

func Strong(text string) (r *HTMLTagBuilder)

Strong "strong": HTMLElement;

func Style

func Style(style string) (r *HTMLTagBuilder)

Style "style": HTMLStyleElement;

func Sub

func Sub(text string) (r *HTMLTagBuilder)

Sub "sub": HTMLElement;

func Summary

func Summary(children ...HTMLComponent) (r *HTMLTagBuilder)

Summary "summary": HTMLElement;

func Sup

func Sup(text string) (r *HTMLTagBuilder)

Sup "sup": HTMLElement;

func Svg

func Svg(children ...HTMLComponent) (r *HTMLTagBuilder)

Svg "svg": HTMLElement;

func Table

func Table(children ...HTMLComponent) (r *HTMLTagBuilder)

Table "table": HTMLTableElement;

func Tag

func Tag(tag string, child ...HTMLComponent) *HTMLTagBuilder
Example

Create a simple div, Text will be escaped by html

banner := "We write html in Go"
comp := Div(
	Text("123<h1>"),
	Textf("Hello, %s", banner),
	Br(),
	Text("new line"),
)
Fprint(os.Stdout, comp, context.TODO())
Output:

<div>123&lt;h1&gt;Hello, We write html in Go<br>new line</div>
Example (Iff)

An example to use If, `Iff` is for body to passed in as an func for the body depends on if condition not to be nil, `If` is for directly passed in HTMLComponent

type Person struct {
	Age int
}
var p *Person

name := "Leon"
comp := Div(
	Iff(p != nil && p.Age > 18, func() HTMLComponent {
		return Div().Text(name + ": Age > 18")
	}).ElseIf(p == nil, func() HTMLComponent {
		return Div().Text("No person named " + name)
	}).Else(func() HTMLComponent {
		return Div().Text(name + ":Age <= 18")
	}),
)
Fprint(os.Stdout, comp, context.TODO())
Output:

<div>
	<div>No person named Leon</div>
</div>
Example (Styles)

An example show how to set styles

comp := Div().
	StyleIf("background-color:red; border:1px solid red;", true).
	StyleIf("color:blue", true)
Fprint(os.Stdout, comp, context.TODO())
Output:

<div style='background-color:red; border:1px solid red; color:blue;'></div>
Example (WithMutipleTypeAttrs)

An example show how to set different type of attributes

type MoreData struct {
	Name  string
	Count int
}
comp := Div().
	Attr("true-attr", true).
	Attr("false-attr", false).
	Attr("int-attr", 1).
	Attr("float-attr", 123.456).
	Attr("str-attr", "the string value").
	Attr("safe-str-value", SafeAttr(`{the shafe value}`)).
	Attr("byte-attr", []byte("byte value")).
	Attr("rune-attr", []rune("rune value")).
	Attr("struct-attr", &MoreData{Name: "felix", Count: 100})
Fprint(os.Stdout, comp, context.TODO())
Output:

<div true-attr int-attr='1' float-attr='123.456000' str-attr='the string value' safe-str-value={the shafe value} byte-attr='byte value' rune-attr='rune value' struct-attr='{"Name":"felix","Count":100}'></div>

func Tbody

func Tbody(children ...HTMLComponent) (r *HTMLTagBuilder)

Tbody "tbody": HTMLTableSectionElement;

func Td

func Td(children ...HTMLComponent) (r *HTMLTagBuilder)

Td "td": HTMLTableDataCellElement;

func Template

func Template(children ...HTMLComponent) (r *HTMLTagBuilder)

Template "template": HTMLTemplateElement;

func Textarea

func Textarea(text string) (r *HTMLTagBuilder)

Textarea "textarea": HTMLTextAreaElement;

func Tfoot

func Tfoot(children ...HTMLComponent) (r *HTMLTagBuilder)

Tfoot "tfoot": HTMLTableSectionElement;

func Th

func Th(text string) (r *HTMLTagBuilder)

Th "th": HTMLTableHeaderCellElement;

func Thead

func Thead(children ...HTMLComponent) (r *HTMLTagBuilder)

Thead "thead": HTMLTableSectionElement;

func Time

func Time(datetime string) (r *HTMLTagBuilder)

Time "time": HTMLTimeElement;

func Title

func Title(text string) (r *HTMLTagBuilder)

Title "title": HTMLTitleElement;

func Tr

func Tr(children ...HTMLComponent) (r *HTMLTagBuilder)

Tr "tr": HTMLTableRowElement;

func Track

func Track(src string) (r *HTMLTagBuilder)

Track "track": HTMLTrackElement;

func U

func U(text string) (r *HTMLTagBuilder)

U "u": HTMLElement;

func Ul

func Ul(children ...HTMLComponent) (r *HTMLTagBuilder)

Ul "ul": HTMLUListElement;

func Var

func Var(text string) (r *HTMLTagBuilder)

Var "var": HTMLElement;

func Video

func Video(children ...HTMLComponent) (r *HTMLTagBuilder)

Video "video": HTMLVideoElement;

func Wbr

func Wbr() (r *HTMLTagBuilder)

Wbr "wbr": HTMLElement;

func (*HTMLTagBuilder) Action

func (b *HTMLTagBuilder) Action(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Alt

func (b *HTMLTagBuilder) Alt(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Append

func (b *HTMLTagBuilder) Append(comp ...HTMLComponent)

func (*HTMLTagBuilder) AppendChildren

func (b *HTMLTagBuilder) AppendChildren(c ...HTMLComponent) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Attr

func (b *HTMLTagBuilder) Attr(vs ...any) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) AttrIf

func (b *HTMLTagBuilder) AttrIf(key, value any, add bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Charset

func (b *HTMLTagBuilder) Charset(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Checked

func (b *HTMLTagBuilder) Checked(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Children

func (b *HTMLTagBuilder) Children(comps ...HTMLComponent) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Class

func (b *HTMLTagBuilder) Class(names ...string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) ClassIf

func (b *HTMLTagBuilder) ClassIf(name string, add bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Content

func (b *HTMLTagBuilder) Content(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Data

func (b *HTMLTagBuilder) Data(vs ...string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Disabled

func (b *HTMLTagBuilder) Disabled(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) For

func (b *HTMLTagBuilder) For(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) GetAttr

func (b *HTMLTagBuilder) GetAttr(key string) *Attr

func (*HTMLTagBuilder) GetChildren

func (b *HTMLTagBuilder) GetChildren() HTMLComponents

func (*HTMLTagBuilder) HasChilds

func (b *HTMLTagBuilder) HasChilds() bool

func (*HTMLTagBuilder) Href

func (b *HTMLTagBuilder) Href(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) ID

func (b *HTMLTagBuilder) ID(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Inline

func (b *HTMLTagBuilder) Inline() (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Method

func (b *HTMLTagBuilder) Method(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Name

func (b *HTMLTagBuilder) Name(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) OmitEndTag

func (b *HTMLTagBuilder) OmitEndTag() (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Placeholder

func (b *HTMLTagBuilder) Placeholder(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Prepend

func (b *HTMLTagBuilder) Prepend(comp ...HTMLComponent)

func (*HTMLTagBuilder) PrependChildren

func (b *HTMLTagBuilder) PrependChildren(c ...HTMLComponent) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Property

func (b *HTMLTagBuilder) Property(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Readonly

func (b *HTMLTagBuilder) Readonly(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Rel

func (b *HTMLTagBuilder) Rel(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) RemoveAttr

func (b *HTMLTagBuilder) RemoveAttr(key ...string) *HTMLTagBuilder

func (*HTMLTagBuilder) Required

func (b *HTMLTagBuilder) Required(v bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Role

func (b *HTMLTagBuilder) Role(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) SetAttr

func (b *HTMLTagBuilder) SetAttr(k string, v any)

func (*HTMLTagBuilder) SetChildren

func (b *HTMLTagBuilder) SetChildren(comps HTMLComponents)

func (*HTMLTagBuilder) Src

func (b *HTMLTagBuilder) Src(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) String

func (b *HTMLTagBuilder) String() string

func (*HTMLTagBuilder) Style

func (b *HTMLTagBuilder) Style(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) StyleIf

func (b *HTMLTagBuilder) StyleIf(v string, add bool) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) TabIndex

func (b *HTMLTagBuilder) TabIndex(v int) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Tag

func (b *HTMLTagBuilder) Tag(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Target

func (b *HTMLTagBuilder) Target(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Text

func (b *HTMLTagBuilder) Text(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Title

func (b *HTMLTagBuilder) Title(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Type

func (b *HTMLTagBuilder) Type(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Value

func (b *HTMLTagBuilder) Value(v string) (r *HTMLTagBuilder)

func (*HTMLTagBuilder) Write

func (b *HTMLTagBuilder) Write(ctx *Context) (err error)

type IfBuilder

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

func If

func If(v bool, comps ...HTMLComponent) (r *IfBuilder)

func (*IfBuilder) Else

func (b *IfBuilder) Else(comps ...HTMLComponent) (r *IfBuilder)

func (*IfBuilder) ElseIf

func (b *IfBuilder) ElseIf(v bool, comps ...HTMLComponent) (r *IfBuilder)

func (*IfBuilder) Write

func (b *IfBuilder) Write(ctx *Context) (_ error)

type IfFuncBuilder

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

func Iff

func Iff(v bool, f func() HTMLComponent) (r *IfFuncBuilder)

func (*IfFuncBuilder) Else

func (b *IfFuncBuilder) Else(f func() HTMLComponent) (r *IfFuncBuilder)

func (*IfFuncBuilder) ElseIf

func (b *IfFuncBuilder) ElseIf(v bool, f func() HTMLComponent) (r *IfFuncBuilder)

func (*IfFuncBuilder) Write

func (b *IfFuncBuilder) Write(ctx *Context) (_ error)

type MutableAttrHTMLComponent

type MutableAttrHTMLComponent interface {
	HTMLComponent
	SetAttr(k string, v any)
}

type RawHTML

type RawHTML string

func (RawHTML) Write

func (s RawHTML) Write(ctx *Context) (err error)

type SafeAttr added in v1.0.1

type SafeAttr string

SafeAttr is a special attr value for unescaped attr value

type TagBuilder

type TagBuilder[T any] struct {
	DOT     T
	HTMLTag *HTMLTagBuilder
}

func (*TagBuilder[T]) Action

func (t *TagBuilder[T]) Action(v string) T

func (*TagBuilder[T]) Alt

func (t *TagBuilder[T]) Alt(v string) T

func (*TagBuilder[T]) AppendChild

func (t *TagBuilder[T]) AppendChild(c ...HTMLComponent) T

func (*TagBuilder[T]) Attr

func (t *TagBuilder[T]) Attr(vs ...any) T

func (*TagBuilder[T]) AttrIf

func (t *TagBuilder[T]) AttrIf(key, value any, add bool) T

func (*TagBuilder[T]) Charset

func (t *TagBuilder[T]) Charset(v string) T

func (*TagBuilder[T]) Checked

func (t *TagBuilder[T]) Checked(v bool) T

func (*TagBuilder[T]) Children

func (t *TagBuilder[T]) Children(c ...HTMLComponent) T

func (*TagBuilder[T]) Class

func (t *TagBuilder[T]) Class(names ...string) T

func (*TagBuilder[T]) ClassIf

func (t *TagBuilder[T]) ClassIf(name string, add bool) T

func (*TagBuilder[T]) Content

func (t *TagBuilder[T]) Content(v string) T

func (*TagBuilder[T]) Data

func (t *TagBuilder[T]) Data(vs ...string) T

func (*TagBuilder[T]) Disabled

func (t *TagBuilder[T]) Disabled(v bool) T

func (*TagBuilder[T]) Dot

func (t *TagBuilder[T]) Dot() T

func (*TagBuilder[T]) For

func (t *TagBuilder[T]) For(v string) T

func (*TagBuilder[T]) GetAttr

func (t *TagBuilder[T]) GetAttr(key string) *Attr

func (*TagBuilder[T]) GetChildren

func (t *TagBuilder[T]) GetChildren() []HTMLComponent

func (*TagBuilder[T]) GetHTMLTagBuilder

func (t *TagBuilder[T]) GetHTMLTagBuilder() *HTMLTagBuilder

func (*TagBuilder[T]) GetTagBuilder

func (t *TagBuilder[T]) GetTagBuilder() *TagBuilder[T]

func (*TagBuilder[T]) Href

func (t *TagBuilder[T]) Href(v string) T

func (*TagBuilder[T]) ID

func (t *TagBuilder[T]) ID(v string) T

func (*TagBuilder[T]) Method

func (t *TagBuilder[T]) Method(v string) T

func (*TagBuilder[T]) Name

func (t *TagBuilder[T]) Name(v string) T

func (*TagBuilder[T]) OmitEndTag

func (t *TagBuilder[T]) OmitEndTag() T

func (*TagBuilder[T]) Placeholder

func (t *TagBuilder[T]) Placeholder(v string) T

func (*TagBuilder[T]) PrependChild

func (t *TagBuilder[T]) PrependChild(c ...HTMLComponent) T

func (*TagBuilder[T]) Property

func (t *TagBuilder[T]) Property(v string) T

func (*TagBuilder[T]) Readonly

func (t *TagBuilder[T]) Readonly(v bool) T

func (*TagBuilder[T]) Rel

func (t *TagBuilder[T]) Rel(v string) T

func (*TagBuilder[T]) RemoveAttr

func (t *TagBuilder[T]) RemoveAttr(key ...string) T

func (*TagBuilder[T]) Required

func (t *TagBuilder[T]) Required(v bool) T

func (*TagBuilder[T]) Role

func (t *TagBuilder[T]) Role(v string) T

func (*TagBuilder[T]) SetAttr

func (t *TagBuilder[T]) SetAttr(k string, v any) T

func (*TagBuilder[T]) SetTag

func (t *TagBuilder[T]) SetTag(v string) T

func (*TagBuilder[T]) Src

func (t *TagBuilder[T]) Src(v string) T

func (*TagBuilder[T]) String

func (t *TagBuilder[T]) String() string

func (*TagBuilder[T]) Style

func (t *TagBuilder[T]) Style(v string) T

func (*TagBuilder[T]) StyleIf

func (t *TagBuilder[T]) StyleIf(v string, add bool) T

func (*TagBuilder[T]) TabIndex

func (t *TagBuilder[T]) TabIndex(v int) T

func (*TagBuilder[T]) TagName

func (t *TagBuilder[T]) TagName() string

func (*TagBuilder[T]) Target

func (t *TagBuilder[T]) Target(v string) T

func (*TagBuilder[T]) Text

func (t *TagBuilder[T]) Text(v string) T

func (*TagBuilder[T]) Title

func (t *TagBuilder[T]) Title(v string) T

func (*TagBuilder[T]) Type

func (t *TagBuilder[T]) Type(v string) T

func (*TagBuilder[T]) Value

func (t *TagBuilder[T]) Value(v string) T

func (*TagBuilder[T]) Write

func (t *TagBuilder[T]) Write(ctx *Context) error

type TagBuilderGetter

type TagBuilderGetter[T any] interface {
	TagGetter
	GetTagBuilder() *TagBuilder[T]
}

type TagGetter

type TagGetter interface {
	GetHTMLTagBuilder() *HTMLTagBuilder
}

type Writer

type Writer interface {
	WriteString(s string) (err error)
	WriteByte(b byte) (err error)
	Write(b []byte) (err error)
}

func ToWriter

func ToWriter(w io.Writer) Writer

type WriterImpl

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

func (*WriterImpl) Write

func (w *WriterImpl) Write(b []byte) (err error)

func (*WriterImpl) WriteByte

func (w *WriterImpl) WriteByte(b byte) (err error)

func (*WriterImpl) WriteString

func (w *WriterImpl) WriteString(s string) (err error)

func (*WriterImpl) Writer

func (w *WriterImpl) Writer() io.Writer

Jump to

Keyboard shortcuts

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