html

package module
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tinywasm/html

HTML element builder API for TinyWasm components.

Overview

tinywasm/html provides declarative HTML element builders for use in TinyWasm components. It was separated from tinywasm/dom so that dom can focus solely on DOM manipulation and syscall.

Installation

go get github.com/tinywasm/html

Usage (dot-import)

import (
    . "github.com/tinywasm/html"  // Div, Span, H1, Nav, Button...
    . "github.com/tinywasm/dom"   // Event, Component, Render, Append
)

func (c *MyComponent) Render() *dom.Element {
    return Div().Class("container").Child(
        H1().Text("Welcome"),
        P().Text("Minimalist UI."),
        Button().Text("Click").On("click", func(e dom.Event) {}),
    )
}

Document shell

Document / DocumentString emit the full HTML shell that sitec writes as index.html: <!DOCTYPE html>, <html lang>, <head> with charset, viewport (width=device-width, initial-scale=1, viewport-fit=cover), optional title/favicon/CSS/JS, and <body>. Empty DocumentOptions{} already includes the viewport — no field to set.

SSR HTML Templates

Components that need a custom SSR template implement RenderHTML() string in their html.go file:

//go:build !wasm
package mycomponent

import . "github.com/tinywasm/html"

func (c *MyComponent) RenderHTML() string {
    return Div().Set(clsRoot.AsAttr()).String()
}

Available Builders

Block: Div, Span, P, Pre, Code, Strong, Small, Mark Headings: H1, H2, H3, H4, H5, H6 Lists: Ul, Ol, Li Semantic: Nav, Section, Main, Article, Header, Footer, Aside, Details, Summary, Dialog, Figure, Figcaption Tables: Table, Thead, Tbody, Tfoot, Tr, Th, Td Form-adjacent: Fieldset, Legend, Label, Button, Canvas, Style, Script Special: A(href), Input(type), Option, SelectedOption, Br, Hr

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func A

func A(href string) *Element

Special

func Article

func Article() *Element

func Aside

func Aside() *Element

func Br

func Br() *Element

func Button

func Button() *Element

func Canvas

func Canvas() *Element

func Code

func Code() *Element

func Details

func Details() *Element

func Dialog

func Dialog() *Element

func Div

func Div() *Element

Block containers

func Document added in v0.0.3

func Document(opts DocumentOptions, body ...Component) *Element

Document builds the complete document shell as *Element (the <html> element). Does not include <!DOCTYPE html> — use DocumentString for the full output.

func DocumentString added in v0.0.3

func DocumentString(opts DocumentOptions, body ...Component) string

DocumentString renders the full HTML document including <!DOCTYPE html>. This is what assetmin writes to disk as index.html.

func Fieldset

func Fieldset() *Element

Form-adjacent (non-form elements)

func Figcaption

func Figcaption() *Element

func Figure

func Figure() *Element
func Footer() *Element

func H1

func H1() *Element

Headings

func H2

func H2() *Element

func H3

func H3() *Element

func H4

func H4() *Element

func H5

func H5() *Element

func H6

func H6() *Element
func Header() *Element

func Hr

func Hr() *Element

func Input

func Input(typ string) *Element

func Label

func Label() *Element

func Legend

func Legend() *Element

func Li

func Li() *Element

func Main

func Main() *Element

func Mark

func Mark() *Element
func Nav() *Element

Semantic layout

func Ol

func Ol() *Element

func Option

func Option(value, text string) *Element

func P

func P() *Element

func Pre

func Pre() *Element

func RewriteAssetURLs added in v0.0.3

func RewriteAssetURLs(htmlStr, newRoot string) string

RewriteAssetURLs rewrites relative href/src attributes in an HTML fragment, keeping only the filename and prepending newRoot. It ignores absolute URLs, rooted paths, and non-file schemes (data:, mailto:, tel:, javascript:). SSR/backend-only: this is excluded from wasm builds (its only caller, assetmin, is backend).

func Script

func Script() *Element

func Section

func Section() *Element

func SelectedOption

func SelectedOption(value, text string) *Element

func Small

func Small() *Element

func Span

func Span() *Element

func Strong

func Strong() *Element

func Style

func Style() *Element

func Summary

func Summary() *Element

func Table

func Table() *Element

Tables

func Tbody

func Tbody() *Element

func Td

func Td() *Element

func Tfoot

func Tfoot() *Element

func Th

func Th() *Element

func Thead

func Thead() *Element

func Tr

func Tr() *Element

func Ul

func Ul() *Element

Lists

Types

type DocumentOptions added in v0.0.3

type DocumentOptions struct {
	Lang       string // default "en"
	Title      string
	CSSURL     string // <link rel="stylesheet">
	JSURL      string // <script src defer>
	FaviconURL string // <link rel="icon">
	Head       string // extra markup for <head> (optional)

	// Description, Canonical and Image are the per-page metadata a search or
	// answer engine reads. They are typed fields rather than markup pushed
	// through Head so that a site emitting many pages cannot silently ship
	// two of them sharing one description — the single most common way a
	// multi-page static site loses its per-page ranking.
	Description string // <meta name="description"> and og:description
	Canonical   string // absolute URL; <link rel="canonical"> and og:url
	Image       string // absolute URL; og:image

	// JSONLD is a schema.org document embedded as
	// <script type="application/ld+json">. It stays a string because its
	// shape is caller-defined (LocalBusiness, MedicalClinic, Service, …) and
	// modelling every schema.org type is not this package's job. Emitted
	// verbatim via dom.Raw/dom.Trust — HTML-escaping it would corrupt the
	// JSON (quotes become &quot;) — so the caller is responsible for it being
	// valid JSON built from their own data, never HTML content from a
	// request/database/third party passed straight through.
	JSONLD string
}

DocumentOptions configures the document shell.

type HTMLProvider

type HTMLProvider interface {
	RenderHTML() string
}

HTMLProvider is an optional capability: components that expose a static SSR HTML template fragment for injection by assetmin.

Implement in a component's html.go file (//go:build !wasm). Most components do NOT need this — only those with a static shell distinct from their dynamic Render() output.

Example in mycomponent/html.go:

//go:build !wasm
package mycomponent
import . "github.com/tinywasm/html"

func (c *MyComponent) RenderHTML() string {
    return Div(clsRoot.AsAttr()).String()
}

For raw static HTML:

func (c *MyComponent) RenderHTML() string {
    return `<div class="root"></div>`
}

type Page added in v0.0.17

type Page struct {
	// Path is the site-absolute route, e.g. "/" or "/especialidades/oftalmologia/".
	// A trailing slash means the compiler writes <path>/index.html so the URL
	// serves without an extension.
	Path string

	// Doc is the head metadata for THIS page. Title, Description and Canonical
	// are what differ per page; CSSURL/JSURL/FaviconURL are normally filled in
	// by the compiler, which is the only layer that knows the built asset names.
	Doc DocumentOptions

	// Body is the rendered markup placed inside <body>.
	Body string
}

Page is one emitted document of a multi-page static site: where it is served from, the head metadata that makes it rank on its own, and its body markup.

A site compiler turns each Page into one file. RenderHTML above is the single-page case (one document at "/"); Page is what a module declares when it owns several routes — a specialty per URL, an article per URL — each needing its own title, description and canonical, which is precisely what a single shared index.html cannot express.

type PagesProvider added in v0.0.17

type PagesProvider interface {
	RenderPages() []Page
}

PagesProvider is an optional capability: a module that owns several routes declares them here instead of (or in addition to) RenderHTML.

Implement in a //go:build !wasm file, like the other SSR producers:

func (m *Module) RenderPages() []Page {
    return []Page{{
        Path: "/especialidades/oftalmologia/",
        Doc:  DocumentOptions{Title: "Oftalmología en Chillán — …", Description: "…"},
        Body: body.String(),
    }}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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