ht

package module
v0.0.0-...-7a055a6 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 4 Imported by: 0

README

HTML Node Builder in Go

This project provides a set of functions to create and manipulate HTML nodes programmatically using Go. It simplifies the process of generating HTML documents by offering a clean and intuitive API.

Features

  • Generate HTML elements with ease.
  • Support for attributes, text, and raw HTML.
  • Render HTML nodes to standard output or files.

How to use

This package is intended to be copied directly into your project (not installed via go get). Copy the ht folder into your repository and import it locally.

Usage

Here is an example of how to use the library:

package main

import (
    "os"

    . "github.com/accentdesign/ht"
    "golang.org/x/net/html"
)

func main() {
    node := Document(
        Doctype("html"),
        Html(
            Lang("en"),
            Head(
                Meta(Charset("utf-8")),
                Meta(Name("viewport"), Content("width=device-width", "initial-scale=1.0")),
                Title(Text("Page")),
                Script(Src("main.js"), Defer()),
                Link(Rel("stylesheet"), Href("style.css")),
            ),
            Body(Class("body")),
        ),
    )
    _ = html.Render(os.Stdout, node)
}

Notes

Some attribute helpers are suffixed with Attr (e.g., LabelAttr, StyleAttr, TitleAttr) to avoid naming conflicts with element constructors (Label, Style, Title). Use these helpers to set the corresponding attribute on an element.

Raw injects unescaped HTML. Only pass trusted content to Raw. Use Text for normal text; it will be escaped by the renderer.

Contract for passing *html.Node as children

When you pass an existing *html.Node (from golang.org/x/net/html) as an argument to Element(...) (or any element helper like Div(...), Span(...), etc.), it is appended using node.AppendChild semantics. That means the child node MUST be detached before you pass it in:

  • The child must have Parent == nil, PrevSibling == nil, and NextSibling == nil.
  • If it already belongs to another tree, detach it first, e.g. child.Parent.RemoveChild(child), then pass it.
  • Alternatively, clone the node if you want to keep the original where it is.

If you pass an attached node, AppendChild will panic (this is behavior from the html package).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func A

func A(args ...any) *h.Node

func Abbr

func Abbr(args ...any) *h.Node

func Action

func Action(v string) html.Attribute

func Address

func Address(args ...any) *h.Node

func Alt

func Alt(v string) html.Attribute

func Aria

func Aria(name, val string) html.Attribute

func Article

func Article(args ...any) *h.Node

func Aside

func Aside(args ...any) *h.Node

func Attr

func Attr(k, v string) html.Attribute

func Autocomplete

func Autocomplete(v string) html.Attribute

func Autofocus

func Autofocus() html.Attribute

func B

func B(args ...any) *h.Node

func Blockquote

func Blockquote(args ...any) *h.Node

func Body

func Body(args ...any) *h.Node

func Br

func Br(args ...any) *h.Node

func Button

func Button(args ...any) *h.Node

func Caption

func Caption(args ...any) *h.Node

func Charset

func Charset(v string) html.Attribute

func Checked

func Checked() html.Attribute

func Cite

func Cite(args ...any) *h.Node

func Class

func Class(v ...string) html.Attribute

func Code

func Code(args ...any) *h.Node

func Col

func Col(args ...any) *h.Node

func Colgroup

func Colgroup(args ...any) *h.Node

func Comment

func Comment(data string) *h.Node

Comment creates and returns a new comment node with the provided data.

func Content

func Content(v ...string) html.Attribute

func Data

func Data(name, val string) html.Attribute

func Dd

func Dd(args ...any) *h.Node

func Defer

func Defer() html.Attribute

func Details

func Details(args ...any) *h.Node

func Dialog

func Dialog(args ...any) *h.Node

func Disabled

func Disabled() html.Attribute

func Div

func Div(args ...any) *h.Node

func Dl

func Dl(args ...any) *h.Node

func Doctype

func Doctype(data string) *h.Node

Doctype creates a new node representing a document type with the specified data.

func Document

func Document(children ...*h.Node) *h.Node

Document creates a new document node and appends the provided child nodes to it.

func Download

func Download(v string) html.Attribute

func Dt

func Dt(args ...any) *h.Node

func Element

func Element(tag a.Atom, args ...any) *h.Node

Element constructs an HTML element node with the given tag and variadic args.

Supported arg types:

  • h.Attribute: added or merged into the element's attributes. When a key has a join strategy (see mergeAttrMap), values are combined instead of replaced.
  • *h.Node: appended as a child of the created element. CONTRACT: The passed node must be detached — i.e. n.Parent == nil, n.PrevSibling == nil, and n.NextSibling == nil. This mirrors golang.org/x/net/html.Node.AppendChild, which will panic if the child already has a parent or siblings. Detach the node from its current parent (e.g. parent.RemoveChild(n)) before passing it here, or clone it if you need to keep the original in place.
  • string, *string, fmt.Stringer, error, or any other type: coerced to text via Text(...).

func Em

func Em(args ...any) *h.Node

func Enctype

func Enctype(v string) html.Attribute

func Fieldset

func Fieldset(args ...any) *h.Node

func Figcaption

func Figcaption(args ...any) *h.Node

func Figure

func Figure(args ...any) *h.Node
func Footer(args ...any) *h.Node

func For

func For(v string) html.Attribute

func Form

func Form(args ...any) *h.Node

func H1

func H1(args ...any) *h.Node

func H2

func H2(args ...any) *h.Node

func H3

func H3(args ...any) *h.Node

func H4

func H4(args ...any) *h.Node

func H5

func H5(args ...any) *h.Node
func Head(args ...any) *h.Node
func Header(args ...any) *h.Node

func Height

func Height(v string) html.Attribute

func Hidden

func Hidden() html.Attribute

func Hr

func Hr(args ...any) *h.Node

func Href

func Href(v string) html.Attribute

func Html

func Html(args ...any) *h.Node

func HxBoost

func HxBoost(v string) html.Attribute

func HxConfirm

func HxConfirm(v string) html.Attribute

func HxDelete

func HxDelete(v string) html.Attribute

func HxGet

func HxGet(v string) html.Attribute

func HxPatch

func HxPatch(v string) html.Attribute

func HxPost

func HxPost(v string) html.Attribute

func HxPushUrl

func HxPushUrl(v string) html.Attribute

func HxPut

func HxPut(v string) html.Attribute

func HxReplaceUrl

func HxReplaceUrl(v string) html.Attribute

func HxSelect

func HxSelect(v string) html.Attribute

func HxSelectOob

func HxSelectOob(v string) html.Attribute

func HxSwap

func HxSwap(v string) html.Attribute

func HxSwapOob

func HxSwapOob(v string) html.Attribute

func HxTarget

func HxTarget(v string) html.Attribute

func HxTrigger

func HxTrigger(v string) html.Attribute

func HxVals

func HxVals(v string) html.Attribute

func I

func I(args ...any) *h.Node

func Id

func Id(v string) html.Attribute

func If

func If(cond bool, node *h.Node) *h.Node

If returns the provided node if the condition is true; otherwise, it returns nil.

func Img

func Img(args ...any) *h.Node

func Input

func Input(args ...any) *h.Node

func Label

func Label(args ...any) *h.Node

func LabelAttr

func LabelAttr(v string) html.Attribute

func Lang

func Lang(v string) html.Attribute

func Legend

func Legend(args ...any) *h.Node

func Li

func Li(args ...any) *h.Node
func Link(args ...any) *h.Node

func Main

func Main(args ...any) *h.Node

func Mark

func Mark(args ...any) *h.Node

func Max

func Max(v string) html.Attribute

func Meta

func Meta(args ...any) *h.Node

func Method

func Method(v string) html.Attribute

func Min

func Min(v string) html.Attribute

func Multiple

func Multiple() html.Attribute

func Name

func Name(v string) html.Attribute
func Nav(args ...any) *h.Node

func Ol

func Ol(args ...any) *h.Node

func Optgroup

func Optgroup(args ...any) *h.Node

func Option

func Option(args ...any) *h.Node

func P

func P(args ...any) *h.Node

func Pattern

func Pattern(v string) html.Attribute

func Placeholder

func Placeholder(v string) html.Attribute

func Pre

func Pre(args ...any) *h.Node

func Raw

func Raw(data string) *h.Node

Raw creates a node with raw HTML content, bypassing any HTML escaping for the supplied input string.

func Readonly

func Readonly() html.Attribute

func Rel

func Rel(v string) html.Attribute

func Required

func Required() html.Attribute

func Role

func Role(v string) html.Attribute

func Script

func Script(args ...any) *h.Node

func Section

func Section(args ...any) *h.Node

func Select

func Select(args ...any) *h.Node

func Selected

func Selected() html.Attribute

func Size

func Size(v string) html.Attribute

func Small

func Small(args ...any) *h.Node

func Span

func Span(args ...any) *h.Node

func Src

func Src(v string) html.Attribute

func Step

func Step(v string) html.Attribute

func Strong

func Strong(args ...any) *h.Node

func Style

func Style(args ...any) *h.Node

func StyleAttr

func StyleAttr(v string) html.Attribute

func Sub

func Sub(args ...any) *h.Node

func Summary

func Summary(args ...any) *h.Node

func Sup

func Sup(args ...any) *h.Node

func Table

func Table(args ...any) *h.Node

func Target

func Target(v string) html.Attribute

func Tbody

func Tbody(args ...any) *h.Node

func Td

func Td(args ...any) *h.Node

func Text

func Text(data string) *h.Node

Text creates a text node with the specified string content and returns a pointer to the node.

func Textarea

func Textarea(args ...any) *h.Node

func Tfoot

func Tfoot(args ...any) *h.Node

func Th

func Th(args ...any) *h.Node

func Thead

func Thead(args ...any) *h.Node

func Title

func Title(args ...any) *h.Node

func TitleAttr

func TitleAttr(v string) html.Attribute

func Tr

func Tr(args ...any) *h.Node

func Type

func Type(v string) html.Attribute

func Ul

func Ul(args ...any) *h.Node

func Value

func Value(v string) html.Attribute

func Width

func Width(v string) html.Attribute

Types

This section is empty.

Directories

Path Synopsis
examples
daisyui command
template command

Jump to

Keyboard shortcuts

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