gtml

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2025 License: MIT Imports: 4 Imported by: 0

README

Test Go Report Card

gtml

No-frills html elements in pure golang.

Usage
import "github.com/accentdesign/gtml"

field := gtml.Div(
    gtml.NA,
    gtml.Label(gtml.Attrs{"for": "email"}, gtml.Text("Email")),
    gtml.Input(gtml.Attrs{
        "class":       "form-input",
        "id":          "email",
        "placeholder": "email@example.com",
        "required":    true,
        "type":        "email",
    }),
    gtml.P(gtml.Attrs{"class": "help-text"}, gtml.Text("Your email address.")),
)

field.Render(context.Background(), os.Stdout)

renders:

<div>
    <label for="email">Email</label>
    <input class="form-input" id="email" placeholder="email@example.com" required type="email">
    <p class="help-text">Your email address.</p>
</div>
Templ

gtml is designed to work with templ. It implements the templ.Component interface.

Easily include them in a templ.Component.

templ Form() {
	<form>
		@field
	</form>
}

Or include a templ.Component in a gtml.Element as a child.

func childTemplate(text string) templ.Component {
    return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
        _, err := io.WriteString(w, "<p>"+text+"</p>")
        return err
    })
}

gtml.Div(gtml.Attrs{"class": "parent"}, childTemplate("Hello, World!"))
CLI

gtml also comes with a CLI tool to generate go from an html file.

install:

go install github.com/accentdesign/gtml/cmd/gtml@latest

usage

gtml -help

generate go from html to file:

gtml generate < input.html > output.go

generate go from html to stdout:

gtml generate < input.html

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// VoidElements is a list of HTML void elements, these don't have children or content.
	VoidElements = map[string]string{
		"area":   ">",
		"base":   ">",
		"br":     ">",
		"col":    ">",
		"embed":  ">",
		"hr":     ">",
		"img":    ">",
		"input":  ">",
		"link":   ">",
		"meta":   ">",
		"source": ">",
		"track":  ">",
		"wbr":    ">",
	}
	// NA is an empty set of attributes.
	NA = Attrs{}
)

Functions

func If added in v0.0.4

func If(condition bool, element templ.Component) templ.Component

If returns the element if the condition is true, otherwise returns an empty element.

func IfElse added in v0.0.4

func IfElse(condition bool, truthyElement, falsyElement templ.Component) templ.Component

IfElse returns the truthy element if the condition is true, otherwise returns the falsy element.

func Map added in v0.0.4

func Map[T any](items []T, transform func(item T) templ.Component) []templ.Component

Map function that applies a transformation function to a list of items and returns the resulting elements.

func Range added in v0.0.4

func Range(start, end int, transform func(i int) templ.Component) []templ.Component

Range function that generates a sequence of numbers and renders elements based on those numbers.

func Version added in v0.0.10

func Version() string

Types

type Attrs

type Attrs templ.Attributes

Attrs is a map of attributes for the element, currently uses templ.Attributes.

type ContentType

type ContentType int

ContentType represents the type of content in an element.

const (
	// TextContent represents text content to be escaped.
	TextContent ContentType = iota
	// RawContent represents raw HTML content with no escaping.
	RawContent
)

type Element

type Element struct {
	Attrs       Attrs             `json:"attrs"`
	Children    []templ.Component `json:"children"`
	Tag         string            `json:"tag"`
	Content     string            `json:"content"`
	ContentType ContentType       `json:"content_type"`
}

Element represents an HTML element.

func A

func A(attrs Attrs, children ...templ.Component) *Element

A creates an anchor element <a></a>.

func Address

func Address(attrs Attrs, children ...templ.Component) *Element

Address creates an address element <address></address>.

func Article

func Article(attrs Attrs, children ...templ.Component) *Element

Article creates an article element <article></article>.

func Aside

func Aside(attrs Attrs, children ...templ.Component) *Element

Aside creates an aside element <aside></aside>.

func B

func B(attrs Attrs, children ...templ.Component) *Element

B creates a bold element <b></b>.

func BR

func BR(attrs Attrs) *Element

BR creates a line break element <br />.

func Blockquote

func Blockquote(attrs Attrs, children ...templ.Component) *Element

Blockquote creates a blockquote element <blockquote></blockquote>.

func Body

func Body(attrs Attrs, children ...templ.Component) *Element

Body creates a body element <body></body>.

func Button

func Button(attrs Attrs, children ...templ.Component) *Element

Button creates a button element <button></button>.

func Caption

func Caption(attrs Attrs, children ...templ.Component) *Element

Caption creates a caption element <caption></caption>.

func DD

func DD(attrs Attrs, children ...templ.Component) *Element

DD creates a definition description element <dd></dd>.

func DL

func DL(attrs Attrs, children ...templ.Component) *Element

DL creates a definition list element <dl></dl>.

func DT

func DT(attrs Attrs, children ...templ.Component) *Element

DT creates a definition term element <dt></dt>.

func Details

func Details(attrs Attrs, children ...templ.Component) *Element

Details creates a details element <details></details>.

func Dialog

func Dialog(attrs Attrs, children ...templ.Component) *Element

Dialog creates a dialog element <dialog></dialog>.

func Div

func Div(attrs Attrs, children ...templ.Component) *Element

Div creates a div element <div></div>.

func Empty added in v0.0.2

func Empty() *Element

Empty creates an empty element.

func Fieldset

func Fieldset(attrs Attrs, children ...templ.Component) *Element

Fieldset creates a fieldset element <fieldset></fieldset>.

func Form

func Form(attrs Attrs, children ...templ.Component) *Element

Form creates a form element <form></form>.

func Fragment added in v0.0.4

func Fragment(children ...templ.Component) *Element

Fragment allows you to create a fragment of elements without a parent element.

func H1

func H1(attrs Attrs, children ...templ.Component) *Element

H1 creates a heading level 1 element <h1></h1>.

func H2

func H2(attrs Attrs, children ...templ.Component) *Element

H2 creates a heading level 2 element <h2></h2>.

func H3

func H3(attrs Attrs, children ...templ.Component) *Element

H3 creates a heading level 3 element <h3></h3>.

func H4

func H4(attrs Attrs, children ...templ.Component) *Element

H4 creates a heading level 4 element <h4></h4>.

func H5

func H5(attrs Attrs, children ...templ.Component) *Element

H5 creates a heading level 5 element <h5></h5>.

func HR

func HR(attrs Attrs) *Element

HR creates a horizontal rule element <hr />.

func Head(attrs Attrs, children ...templ.Component) *Element

Head creates a head element <head></head>.

func Header(attrs Attrs, children ...templ.Component) *Element

Header creates a header element <header></header>.

func Html

func Html(attrs Attrs, children ...templ.Component) *Element

Html creates an html element <html></html>.

func I

func I(attrs Attrs, children ...templ.Component) *Element

I creates an italic element <i></i>.

func Img

func Img(attrs Attrs) *Element

Img creates an image element <img />.

func Input

func Input(attrs Attrs) *Element

Input creates an input element <input />.

func LI

func LI(attrs Attrs, children ...templ.Component) *Element

LI creates a list item element <li></li>.

func Label

func Label(attrs Attrs, children ...templ.Component) *Element

Label creates a label element <label></label>.

func Legend

func Legend(attrs Attrs, children ...templ.Component) *Element

Legend creates a legend element <legend></legend>.

func Link(attrs Attrs) *Element

Link creates a link element <link />.

func Main

func Main(attrs Attrs, children ...templ.Component) *Element

Main creates a main element <main></main>.

func Meta

func Meta(attrs Attrs) *Element

Meta creates a meta element <meta />.

func Nav(attrs Attrs, children ...templ.Component) *Element

Nav creates a nav element <nav></nav>.

func New added in v0.0.7

func New(tag string, attrs Attrs, children ...templ.Component) *Element

New creates an element with the specified tag.

func OL

func OL(attrs Attrs, children ...templ.Component) *Element

OL creates an ordered list element <ol></ol>.

func Optgroup

func Optgroup(attrs Attrs, children ...templ.Component) *Element

Optgroup creates an option group element <optgroup></optgroup>.

func Option

func Option(attrs Attrs, children ...templ.Component) *Element

Option creates an option element <option></option>.

func P

func P(attrs Attrs, children ...templ.Component) *Element

P creates a paragraph element <p></p>.

func Raw

func Raw(html string) *Element

Raw creates an element with raw HTML content. Warning: this elements cannot have children and content is not escaped.

func Section

func Section(attrs Attrs, children ...templ.Component) *Element

Section creates a section element <section></section>.

func Select

func Select(attrs Attrs, children ...templ.Component) *Element

Select creates a select element <select></select>.

func Small

func Small(attrs Attrs, children ...templ.Component) *Element

Small creates a small element <small></small>.

func Span

func Span(attrs Attrs, children ...templ.Component) *Element

Span creates a span element <span></span>.

func Strong

func Strong(attrs Attrs, children ...templ.Component) *Element

Strong creates a strong element <strong></strong>.

func Summary

func Summary(attrs Attrs, children ...templ.Component) *Element

Summary creates a summary element <summary></summary>.

func TBody

func TBody(attrs Attrs, children ...templ.Component) *Element

TBody creates a table body element <tbody></tbody>.

func TD

func TD(attrs Attrs, children ...templ.Component) *Element

TD creates a table data element <td></td>.

func TFoot

func TFoot(attrs Attrs, children ...templ.Component) *Element

TFoot creates a table footer element <tfoot></tfoot>.

func TH

func TH(attrs Attrs, children ...templ.Component) *Element

TH creates a table header element <th></th>.

func THead

func THead(attrs Attrs, children ...templ.Component) *Element

THead creates a table header element <thead></thead>.

func TR

func TR(attrs Attrs, children ...templ.Component) *Element

TR creates a table row element <tr></tr>.

func Table

func Table(attrs Attrs, children ...templ.Component) *Element

Table creates a table element <table></table>.

func Text

func Text(text string) *Element

Text creates an element with text content. Warning: this elements cannot have children and content will be escaped.

func Textarea

func Textarea(attrs Attrs, children ...templ.Component) *Element

Textarea creates a textarea element <textarea></textarea>.

func Title

func Title(attrs Attrs, children ...templ.Component) *Element

Title creates a title element <title></title>.

func UL

func UL(attrs Attrs, children ...templ.Component) *Element

UL creates an unordered list element <ul></ul>.

func (*Element) AddChildren

func (e *Element) AddChildren(children ...templ.Component)

AddChildren adds children *Element's to this element.

func (*Element) Render

func (e *Element) Render(ctx context.Context, w io.Writer) error

Render renders the Element to the writer.

Directories

Path Synopsis
cmd
gtml command
examples
conditional command
custom command
form command
page command
paginate command
static command
table command
templ-attr-kv command
templ-component command
templ-js command
templ: version: v0.3.819
templ: version: v0.3.819

Jump to

Keyboard shortcuts

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