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
| Name | Declaration | Use |
|---|---|---|
Node | type Node interface { Render(ctx context.Context, w io.Writer) error } | Renderable value returned by every component. |
Func | type Func func(ctx context.Context, w io.Writer) error | Adapt a render function to Node; its Render method calls the function. |
Trusted values
| Name | Declaration | Use |
|---|---|---|
Raw | func Raw(html string) Node | Render trusted HTML verbatim. |
RawURL | type RawURL string | Trust a URL scheme while retaining attribute escaping. |
RawJS | type RawJS string | Trust JavaScript and bypass JavaScript encoding. |
RawCSS | type RawCSS string | Trust 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
| Name | Signature | Use |
|---|---|---|
Val | func Val(v any) Node | Box a supported Go value as a Node. |
Text | func Text(s string) Node | Create an HTML-escaped text node. |
Fragment | func Fragment(nodes ...Node) Node | Render 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
| Name | Declaration or signature | Use |
|---|---|---|
Attr | type Attr struct { Key string; Value any } | One ordered attribute pair. |
Attrs | type Attrs []Attr | Ordered attribute bag. |
AttrMap.ToAttrs | func (m AttrMap) ToAttrs() Attrs | Convert a map, sorted by key. |
Attrs methods
Read values
| Name | Signature | Use |
|---|---|---|
Class | func (a Attrs) Class() string | Join class values. |
Style | func (a Attrs) Style() string | Join style values. |
Get | func (a Attrs) Get(key string) (any, bool) | Get the last value. |
Has | func (a Attrs) Has(key string) bool | Did the caller supply the key? Present even when its value is false. |
Bool | func (a Attrs) Bool(key string) bool | Boolean 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
| Name | Signature | Use |
|---|---|---|
Without | func (a Attrs) Without(keys ...string) Attrs | Copy without keys. |
Take | func (a Attrs) Take(key string) (any, Attrs) | Get and remove a key. |
Merge | func (a Attrs) Merge(other Attrs) Attrs | Merge bags; class and style compose. |
See Attributes for spread order and Styling for class and style precedence.