Skip to content

Runtime helpers ​

These are the github.com/gsxhq/gsx types and functions commonly called from application code. See Attributes, Styling, and Escaping for their syntax and safety rules.

Core ​

NameDeclarationUse
Nodetype Node interface { Render(ctx context.Context, w io.Writer) error }Renderable value returned by every component.
Functype Func func(ctx context.Context, w io.Writer) errorAdapt a render function to Node; its Render method calls the function.

Trusted values ​

NameDeclarationUse
Rawfunc Raw(html string) NodeRender trusted HTML verbatim.
RawURLtype RawURL stringTrust a URL scheme while retaining attribute escaping.
RawJStype RawJS stringTrust JavaScript and bypass JavaScript encoding.
RawCSStype RawCSS stringTrust CSS and bypass CSS value filtering.

Each entry crosses a trust boundary. Pass only content you control or have validated for that context; see Escaping.

Node values ​

NameSignatureUse
Valfunc Val(v any) NodeBox a supported Go value as a Node.
Textfunc Text(s string) NodeCreate an HTML-escaped text node.
Fragmentfunc Fragment(nodes ...Node) NodeRender several nodes in order without a wrapper element.

Val accepts nil, Node, []Node, string, []byte, fmt.Stringer, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64. Other values return an error from Render. A named scalar without a supported interface must be converted to its built-in type before boxing it.

Attribute bags ​

Bag types ​

NameDeclaration or signatureUse
Attrtype Attr struct { Key string; Value any }One ordered attribute pair.
Attrstype Attrs []AttrOrdered attribute bag.
AttrMap.ToAttrsfunc (m AttrMap) ToAttrs() AttrsConvert a map, sorted by key.

Attrs methods ​

Read values ​

NameSignatureUse
Classfunc (a Attrs) Class() stringJoin class values.
Stylefunc (a Attrs) Style() stringJoin style values.
Getfunc (a Attrs) Get(key string) (any, bool)Get the last value.
Hasfunc (a Attrs) Has(key string) boolDid the caller supply the key? Present even when its value is false.
Boolfunc (a Attrs) Bool(key string) boolBoolean state of a key, as the spread renders it: Toggle/bool decide, nil or absent is false, anything else is true.

Presence vs state. disabled={expr} is in the bag whatever expr is; the false pair must stay so a later spread can override an earlier true, and so a component can tell "caller said false" from "caller said nothing". Has answers the second question and is the right guard for defaults: { if !attrs.Has("type") { type="button" } } lets a caller override with any value, including false. Bool answers "is it on?" for styling by state: disabled={false} is false, and so is aria-pressed={false} even though that one renders as aria-pressed="false" (see Boolean attributes). Get returns the raw value when you need to see the distinction yourself.

Keys are HTML attribute names and every accessor matches them as the browser does, ignoring ASCII case: Has("href") sees an HREF key. Get("style") returns the style already CSS-filtered, as gsx.RawCSS, so it can go back into a bag.

Transform bags ​

NameSignatureUse
Withoutfunc (a Attrs) Without(keys ...string) AttrsCopy without keys.
Takefunc (a Attrs) Take(key string) (any, Attrs)Get and remove a key.
Mergefunc (a Attrs) Merge(other Attrs) AttrsMerge bags; class and style compose.

See Attributes for spread order and Styling for class and style precedence.