yahw

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 3 Imported by: 0

README

Yet Another HTML Wrapper

Example

Check the example in example/ folder.

package main

import (
	"io"
	"net/http"

	. "github.com/vizualni/yahw"
)

type MyCustomButton struct {
	Text            string
	BackgroundColor string
}

func (m MyCustomButton) TagRender(w io.Writer) error {
	return Button(
		Attr("style", "background-color: "+m.BackgroundColor),
	).X(
		Text(m.Text),
	).TagRender(w)
}

func MyCustomInput(name, placeholder string) TagRenderer {
	return Input(
		Attr("name", name),
		Attr("placeholder", placeholder),
	)
}

func MyCommonAttributes(link string) AttrRenderer {
	return AttrSlice{Attr("id", "my-id"), Classes("my-1 my-2 my-1"), Attr("href", link)}
}

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		root := NewHTML5Doctype().X(
			HTML().X(
				Head().X(
					Title().X(Text("My Custom Button Example")),
					Style().X(Text("button { padding: 10px; border: none; }")),
				),
				Body().X(
					MyCustomButton{
						Text:            "Click me!",
						BackgroundColor: "red",
					},
					Br(),
					MyCustomButton{
						Text:            "No, click me!",
						BackgroundColor: "green",
					},
					Br(),
					MyCustomInput("name", "Enter your name"),
					Br(),
					MyCustomInput("email", "Enter your email"),
					Br(),
					A(MyCommonAttributes("https://example1.com")).X(Text("Click me!")),
					Br(),
					A(MyCommonAttributes("https://example2.com")).X(Text("No, click me!")),
				),
			),
		)

		root.TagRender(w)
	})

	if err := http.ListenAndServe("127.0.0.1:8585", nil); err != nil {
		panic(err)
	}
}

Run go run ./example and visit http://localhost:8585/

Why?

It's a common pattern I am using in my pet projects. I thought I might share it as it could be useful for others.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttrBuilder

func AttrBuilder(key string) func(string) Attribute

func SelfClosingTagBuilder

func SelfClosingTagBuilder(tagName string) func(...AttrRenderer) SelfClosingTag

func TagBuilder

func TagBuilder(tagName string) func(...AttrRenderer) Tag

Types

type AttrRenderer

type AttrRenderer interface {
	AttrRender(w io.Writer) error
}

type AttrSlice added in v0.1.0

type AttrSlice []AttrRenderer

func (AttrSlice) AttrRender added in v0.1.0

func (a AttrSlice) AttrRender(w io.Writer) error

type Attribute

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

func Accept added in v0.2.6

func Accept(accept string) Attribute

func AccessKey

func AccessKey(accessKey string) Attribute

func Action

func Action(action string) Attribute

func Alt

func Alt(alt string) Attribute

func Aria

func Aria(name string, value string) Attribute

func Attr

func Attr(key, value string) Attribute

func AutoComplete

func AutoComplete(autoComplete string) Attribute

func AutoFocus

func AutoFocus(autoFocus string) Attribute

func AutoSave

func AutoSave(autoSave string) Attribute

func Axis

func Axis(axis string) Attribute

func Charset

func Charset(charset string) Attribute

func Class

func Class(class string) Attribute

func ColSpan

func ColSpan(colSpan string) Attribute

func Content

func Content(content string) Attribute

func ContentEditable

func ContentEditable(contentEditable string) Attribute

func ContextMenu

func ContextMenu(contextMenu string) Attribute

func Coords

func Coords(coords string) Attribute

func CrossOrigin

func CrossOrigin(crossOrigin string) Attribute

func DataAttr

func DataAttr(name string, value string) Attribute

func DateTime

func DateTime(datetime string) Attribute

func Default

func Default(default_ string) Attribute

func Dir

func Dir(dir string) Attribute

func Download

func Download(download string) Attribute

func Draggable

func Draggable(draggable string) Attribute

func DropZone

func DropZone(dropZone string) Attribute

func EncType added in v0.2.7

func EncType(encType string) Attribute

func For

func For(for_ string) Attribute

func FormAction

func FormAction(formAction string) Attribute

func FormAttr added in v0.2.4

func FormAttr(form string) Attribute

func FormEncType

func FormEncType(formEncType string) Attribute

func FormMethod

func FormMethod(formMethod string) Attribute

func FormNoValidate

func FormNoValidate(formNoValidate string) Attribute

func FormTarget

func FormTarget(formTarget string) Attribute

func Headers

func Headers(headers string) Attribute

func HeadersAttr

func HeadersAttr(headers string) Attribute

func Height

func Height(height string) Attribute

func Hidden

func Hidden(hidden string) Attribute

func High

func High(high string) Attribute

func Href

func Href(href string) Attribute

func HrefLang

func HrefLang(hrefLang string) Attribute

func HttpEquiv

func HttpEquiv(httpEquiv string) Attribute

func ID

func ID(id string) Attribute

func Integrity

func Integrity(integrity string) Attribute

func IsMap

func IsMap(isMap string) Attribute

func KeyType

func KeyType(keytype string) Attribute

func Kind

func Kind(kind string) Attribute

func LabelAttr

func LabelAttr(label string) Attribute

func Lang

func Lang(lang string) Attribute

func List

func List(list string) Attribute

func LongDesc

func LongDesc(longDesc string) Attribute

func Low

func Low(low string) Attribute

func Max

func Max(max string) Attribute

func MaxLength

func MaxLength(maxLength string) Attribute

func Media

func Media(media string) Attribute

func Method

func Method(method string) Attribute

func Min

func Min(min string) Attribute

func MinLength

func MinLength(minLength string) Attribute

func Multiple

func Multiple(multiple string) Attribute

func Name

func Name(name string) Attribute

func OnAbort

func OnAbort(handler string) Attribute

func OnBlur

func OnBlur(handler string) Attribute

func OnChange

func OnChange(handler string) Attribute

func OnClick

func OnClick(handler string) Attribute

func OnError

func OnError(handler string) Attribute

func OnFocus

func OnFocus(handler string) Attribute

func OnKeyDown

func OnKeyDown(handler string) Attribute

func OnKeyPress

func OnKeyPress(handler string) Attribute

func OnKeyUp

func OnKeyUp(handler string) Attribute

func OnLoad

func OnLoad(handler string) Attribute

func OnMouseDown

func OnMouseDown(handler string) Attribute

func OnMouseOut

func OnMouseOut(handler string) Attribute

func OnMouseOver

func OnMouseOver(handler string) Attribute

func OnMouseUp

func OnMouseUp(handler string) Attribute

func OnReset

func OnReset(handler string) Attribute

func OnResize

func OnResize(handler string) Attribute

func OnScroll

func OnScroll(handler string) Attribute

func OnSelect

func OnSelect(handler string) Attribute

func OnSubmit

func OnSubmit(handler string) Attribute

func OnUnload

func OnUnload(handler string) Attribute

func Optimum

func Optimum(optimum string) Attribute

func Pattern

func Pattern(pattern string) Attribute

func Ping

func Ping(ping string) Attribute

func Placeholder

func Placeholder(placeholder string) Attribute

func ReferrerPolicy

func ReferrerPolicy(referrerPolicy string) Attribute

func Rel

func Rel(rel string) Attribute

func Rev

func Rev(rev string) Attribute

func Role

func Role(role string) Attribute

func RowSpan

func RowSpan(rowSpan string) Attribute

func Scheme

func Scheme(scheme string) Attribute

func Scope

func Scope(scope string) Attribute

func Shape

func Shape(shape string) Attribute

func Size

func Size(size string) Attribute

func Sizes

func Sizes(sizes string) Attribute

func SpellCheck

func SpellCheck(spellCheck string) Attribute

func Src

func Src(src string) Attribute

func SrcLang

func SrcLang(srclang string) Attribute

func SrcSet

func SrcSet(srcSet string) Attribute

func Step

func Step(step string) Attribute

func StyleAttr

func StyleAttr(style string) Attribute

func TabIndex

func TabIndex(tabIndex string) Attribute

func Target

func Target(target string) Attribute

func TitleAttr

func TitleAttr(title string) Attribute

Additional common attributes

func Translate

func Translate(translate string) Attribute

func Type

func Type(type_ string) Attribute

func UseMap

func UseMap(useMap string) Attribute

func Value

func Value(value string) Attribute

func Width

func Width(width string) Attribute

func (Attribute) AttrRender

func (a Attribute) AttrRender(w io.Writer) error

type Classes

type Classes string

func (Classes) AttrRender

func (c Classes) AttrRender(w io.Writer) error

type HTML5Doctype

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

func NewHTML5Doctype

func NewHTML5Doctype(cs ...TagRenderer) HTML5Doctype

func (HTML5Doctype) TagRender added in v0.0.2

func (t HTML5Doctype) TagRender(w io.Writer) error

func (HTML5Doctype) X

func (t HTML5Doctype) X(children ...TagRenderer) HTML5Doctype

type IfElseAttr added in v0.3.0

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

func IfAttr added in v0.3.0

func IfAttr(cond bool, then AttrRenderer) IfElseAttr

func (IfElseAttr) AttrRender added in v0.3.0

func (t IfElseAttr) AttrRender(w io.Writer) error

func (IfElseAttr) Else added in v0.3.0

func (ie IfElseAttr) Else(els AttrRenderer) IfElseAttr

type IfElseTag added in v0.3.0

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

func If added in v0.3.0

func If(cond bool, then TagRenderer) IfElseTag

func (IfElseTag) Else added in v0.3.0

func (ie IfElseTag) Else(els TagRenderer) IfElseTag

func (IfElseTag) TagRender added in v0.3.0

func (t IfElseTag) TagRender(w io.Writer) error

type NoValAttribute added in v0.2.0

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

func Checked

func Checked() NoValAttribute

func Disabled

func Disabled() NoValAttribute

func NoValAttr added in v0.2.0

func NoValAttr(key string) NoValAttribute

func NoValidate

func NoValidate() NoValAttribute

func ReadOnly

func ReadOnly() NoValAttribute

func Required

func Required() NoValAttribute

func (NoValAttribute) AttrRender added in v0.2.0

func (a NoValAttribute) AttrRender(w io.Writer) error

type SelfClosingTag

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

func Area

func Area(attrs ...AttrRenderer) SelfClosingTag

func Base

func Base(attrs ...AttrRenderer) SelfClosingTag

func Br

func Br(attrs ...AttrRenderer) SelfClosingTag

func Col

func Col(attrs ...AttrRenderer) SelfClosingTag

func Embed

func Embed(attrs ...AttrRenderer) SelfClosingTag

func Hr

func Hr(attrs ...AttrRenderer) SelfClosingTag

func Img

func Img(attrs ...AttrRenderer) SelfClosingTag

func Input

func Input(attrs ...AttrRenderer) SelfClosingTag
func Link(attrs ...AttrRenderer) SelfClosingTag

func Meta

func Meta(attrs ...AttrRenderer) SelfClosingTag

func Param

func Param(attrs ...AttrRenderer) SelfClosingTag

func Source

func Source(attrs ...AttrRenderer) SelfClosingTag

func Track

func Track(attrs ...AttrRenderer) SelfClosingTag

func Wbr

func Wbr(attrs ...AttrRenderer) SelfClosingTag

func (SelfClosingTag) TagRender

func (t SelfClosingTag) TagRender(w io.Writer) error

type Tag

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

func A

func A(attrs ...AttrRenderer) Tag

func Abbr

func Abbr(attrs ...AttrRenderer) Tag

func Address

func Address(attrs ...AttrRenderer) Tag

func Article

func Article(attrs ...AttrRenderer) Tag

func Aside

func Aside(attrs ...AttrRenderer) Tag

func Audio

func Audio(attrs ...AttrRenderer) Tag

func B

func B(attrs ...AttrRenderer) Tag

func Bdi

func Bdi(attrs ...AttrRenderer) Tag

func Bdo

func Bdo(attrs ...AttrRenderer) Tag

func Blockquote

func Blockquote(attrs ...AttrRenderer) Tag

func Body

func Body(attrs ...AttrRenderer) Tag

func Button

func Button(attrs ...AttrRenderer) Tag

func Canvas

func Canvas(attrs ...AttrRenderer) Tag

func Caption

func Caption(attrs ...AttrRenderer) Tag

func Cite

func Cite(attrs ...AttrRenderer) Tag

func Code

func Code(attrs ...AttrRenderer) Tag

func Colgroup

func Colgroup(attrs ...AttrRenderer) Tag

func Data

func Data(attrs ...AttrRenderer) Tag

func Datalist

func Datalist(attrs ...AttrRenderer) Tag

func Dd

func Dd(attrs ...AttrRenderer) Tag

func Del

func Del(attrs ...AttrRenderer) Tag

func Details

func Details(attrs ...AttrRenderer) Tag

func Dfn

func Dfn(attrs ...AttrRenderer) Tag

func Dialog

func Dialog(attrs ...AttrRenderer) Tag

func Div

func Div(attrs ...AttrRenderer) Tag

func Dl

func Dl(attrs ...AttrRenderer) Tag

func Dt

func Dt(attrs ...AttrRenderer) Tag

func Em

func Em(attrs ...AttrRenderer) Tag

func Fieldset

func Fieldset(attrs ...AttrRenderer) Tag

func Figcaption

func Figcaption(attrs ...AttrRenderer) Tag

func Figure

func Figure(attrs ...AttrRenderer) Tag
func Footer(attrs ...AttrRenderer) Tag

func Form

func Form(attrs ...AttrRenderer) Tag

func H1

func H1(attrs ...AttrRenderer) Tag

func H2

func H2(attrs ...AttrRenderer) Tag

func H3

func H3(attrs ...AttrRenderer) Tag

func H4

func H4(attrs ...AttrRenderer) Tag

func H5

func H5(attrs ...AttrRenderer) Tag

func H6

func H6(attrs ...AttrRenderer) Tag

func HTML

func HTML(attrs ...AttrRenderer) Tag
func Head(attrs ...AttrRenderer) Tag
func Header(attrs ...AttrRenderer) Tag

func I

func I(attrs ...AttrRenderer) Tag

func Iframe

func Iframe(attrs ...AttrRenderer) Tag

func Ins

func Ins(attrs ...AttrRenderer) Tag

func Kbd

func Kbd(attrs ...AttrRenderer) Tag

func Label

func Label(attrs ...AttrRenderer) Tag

func Legend

func Legend(attrs ...AttrRenderer) Tag

func Li

func Li(attrs ...AttrRenderer) Tag

func Main

func Main(attrs ...AttrRenderer) Tag

func Map

func Map(attrs ...AttrRenderer) Tag

func Mark

func Mark(attrs ...AttrRenderer) Tag

func Meter

func Meter(attrs ...AttrRenderer) Tag
func Nav(attrs ...AttrRenderer) Tag

func NewTag added in v0.3.2

func NewTag(tagName string) Tag

func Noscript

func Noscript(attrs ...AttrRenderer) Tag

func Object

func Object(attrs ...AttrRenderer) Tag

func Ol

func Ol(attrs ...AttrRenderer) Tag

func Optgroup

func Optgroup(attrs ...AttrRenderer) Tag

func Option

func Option(attrs ...AttrRenderer) Tag

func Output

func Output(attrs ...AttrRenderer) Tag

func P

func P(attrs ...AttrRenderer) Tag

func Picture

func Picture(attrs ...AttrRenderer) Tag

func Pre

func Pre(attrs ...AttrRenderer) Tag

func Progress

func Progress(attrs ...AttrRenderer) Tag

func Q

func Q(attrs ...AttrRenderer) Tag

func Rp

func Rp(attrs ...AttrRenderer) Tag

func Rt

func Rt(attrs ...AttrRenderer) Tag

func Ruby

func Ruby(attrs ...AttrRenderer) Tag

func S

func S(attrs ...AttrRenderer) Tag

func Samp

func Samp(attrs ...AttrRenderer) Tag

func Script

func Script(attrs ...AttrRenderer) Tag

func Section

func Section(attrs ...AttrRenderer) Tag

func Select

func Select(attrs ...AttrRenderer) Tag

func Slot

func Slot(attrs ...AttrRenderer) Tag

func Small

func Small(attrs ...AttrRenderer) Tag

func Span

func Span(attrs ...AttrRenderer) Tag

func Strong

func Strong(attrs ...AttrRenderer) Tag

func Style

func Style(attrs ...AttrRenderer) Tag

func Sub

func Sub(attrs ...AttrRenderer) Tag

func Summary

func Summary(attrs ...AttrRenderer) Tag

func Sup

func Sup(attrs ...AttrRenderer) Tag

func Table

func Table(attrs ...AttrRenderer) Tag

func Tbody

func Tbody(attrs ...AttrRenderer) Tag

func Td

func Td(attrs ...AttrRenderer) Tag

func Template

func Template(attrs ...AttrRenderer) Tag

func Textarea

func Textarea(attrs ...AttrRenderer) Tag

func Tfoot

func Tfoot(attrs ...AttrRenderer) Tag

func Th

func Th(attrs ...AttrRenderer) Tag

func Thead

func Thead(attrs ...AttrRenderer) Tag

func Time

func Time(attrs ...AttrRenderer) Tag

func Title

func Title(attrs ...AttrRenderer) Tag

func Tr

func Tr(attrs ...AttrRenderer) Tag

func U

func U(attrs ...AttrRenderer) Tag

func Ul

func Ul(attrs ...AttrRenderer) Tag

func Var

func Var(attrs ...AttrRenderer) Tag

func Video

func Video(attrs ...AttrRenderer) Tag

func (Tag) Attrs added in v0.3.2

func (t Tag) Attrs(attrs ...AttrRenderer) Tag

func (Tag) TagRender

func (t Tag) TagRender(w io.Writer) error

func (Tag) X

func (t Tag) X(children ...TagRenderer) Tag

type TagRenderer

type TagRenderer interface {
	TagRender(w io.Writer) error
}

type TagSlice added in v0.1.0

type TagSlice []TagRenderer

func (TagSlice) TagRender added in v0.1.0

func (t TagSlice) TagRender(w io.Writer) error

type Text

type Text string

func (Text) TagRender

func (t Text) TagRender(w io.Writer) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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