Documentation
¶
Index ¶
- func NewPrinter(w io.Writer, opts ...Option) gox.Printer
- func Parallel() gox.Proxy
- func ProxyMod(mod gox.Modify) gox.Proxy
- func WriterError(err error) (error, bool)
- type Classes
- func (c Classes) Add(classes ...string) Classes
- func (c Classes) Clone() Classes
- func (c Classes) Filter(classes ...string) Classes
- func (c Classes) Join(classes ...Classes) Classes
- func (c Classes) Modify(ctx context.Context, tag string, atts gox.Attrs) error
- func (c Classes) Mutate(name string, prev any) any
- func (c Classes) Output(w io.Writer) error
- func (c Classes) Proxy(cur gox.Cursor, el gox.Elem) error
- func (c Classes) Remove(classes ...string) Classes
- func (c Classes) String() string
- type CompParallel
- type Option
- type WriteErr
- type WriterToCloser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewPrinter ¶
NewPrinter returns a GoX printer that renders gox.Comp and gox.Elem values with support for Parallel subtrees.
To run part of a template in parallel, proxy that fragment to ~>(goxx.Parallel()). NewPrinter schedules those marked fragments on its worker pool while the rest of the template continues rendering.
Output is buffered per parallel branch and drained to w in source order. By default the printer uses seven workers and gox.NewPrinter for sequential chunks. Use WithWorkers to tune or remove the worker limit. A negative worker count panics.
NewPrinter is useful when you want to pass a printer to gox.Elem.Print. In HTTP handlers, prefer Render: it returns buffered output first, so you can set headers and a custom success status after rendering succeeds and before the response body is written.
Use WriterError to check whether elem.Print failed because of the final io.Writer. Other render errors are returned before buffered output is written to w.
func Parallel ¶
Parallel returns a proxy that schedules elem as a parallel subtree when it is rendered by NewPrinter.
Use it for independent fragments that may wait on database queries, external API calls, or other slow work.
Output order stays the same as the template order. When used with another printer, the subtree renders sequentially and logs a misuse warning.
func ProxyMod ¶
ProxyMod returns a proxy that applies mod to the first element rendered by elem.
Use it to build helpers that attach attributes or attribute modifiers to another element or component. If elem starts with a component or container, ProxyMod carries mod forward until it reaches the first element. Text or other non-element output before that element is an error. The modifier is applied once; later sibling elements are left unchanged.
Parallel markers are preserved, so the wrapped subtree can still be scheduled by NewPrinter.
func WriterError ¶
WriterError returns the underlying writer error from err.
It reports false when err is not, and does not wrap, a WriteErr.
Types ¶
type Classes ¶
type Classes struct {
// contains filtered or unexported fields
}
Classes describes class names to add and filter out.
Classes values are immutable: Add, Remove, Filter, Join, and Clone return a new value and leave the receiver unchanged.
func Class ¶
Class builds a class modifier from one or more class strings.
Each argument is split with strings.Fields, so Class("a", "b c") and Class("a b c") produce the same classes. The returned value can be used as a class attribute value, as an attribute modifier, or as a proxy before an element/component.
func (Classes) Add ¶
Add returns a new Classes value with classes appended.
Arguments are split like Class, so Add("a", "b c") adds three classes.
func (Classes) Filter ¶ added in v0.1.4
Filter returns a new Classes value that omits matching classes from output.
Removed classes are filtered regardless of whether they were added before or after Filter was called.
func (Classes) Join ¶
Join returns a new Classes value that combines several class modifiers.
Both added and filtered class names are preserved, so filters from joined values still affect the final rendered class list.
func (Classes) Remove ¶
Remove returns a new Classes value with matching currently-added classes removed.
Removed classes are not remembered: the same class can be added again later. Use Filter when matching classes should be omitted from final output even if they are added later or come from a joined Classes value.
type CompParallel ¶
CompParallel marks an Elem for parallel rendering by NewPrinter.
Prefer Parallel in templates and component code. CompParallel is exported so printer and proxy integrations can preserve the marker when wrapping or forwarding components.
func (CompParallel) Main ¶
func (p CompParallel) Main() gox.Elem
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a printer created by NewPrinter.
func WithFlat ¶ added in v0.1.3
func WithFlat() Option
WithFlat makes NewPrinter expand ordinary component jobs before they reach the base printer.
Use it with WithPrinter when your custom printer wants to handle the actual content stream and does not want to render or inspect gox.JobComp values itself. Parallel markers are still handled by NewPrinter.
func WithPrinter ¶ added in v0.1.3
WithPrinter lets you add your own printer extension to the rendering pipeline.
NewPrinter calls f for each sequential chunk or parallel branch, passing the branch-local buffer that printer should write to. Use WithFlat if your printer wants expanded content instead of gox.JobComp values.
By default, NewPrinter uses gox.NewPrinter, which renders jobs directly to the provided io.Writer.
func WithWorkers ¶ added in v0.1.3
WithWorkers sets the maximum number of parallel background worker tasks.
The default is seven background workers, plus the caller goroutine, for eight render tasks in total. Passing zero skips the worker pool and starts plain goroutines for parallel tasks. Passing a negative value causes NewPrinter to panic.
type WriteErr ¶
type WriteErr struct {
// Err is the underlying writer error.
Err error
}
WriteErr wraps an error returned by the final io.Writer.
It is returned after rendering has succeeded and NewPrinter is draining its buffered output to the writer passed to NewPrinter.
type WriterToCloser ¶ added in v0.1.3
WriterToCloser is buffered printer output returned by Render.
WriteTo writes the buffered output once and releases it. After WriteTo or Close, later WriteTo calls return an error. Close releases the output without writing it; call Close when you decide not to write the rendered output. Close is safe to call more than once.
func Render ¶ added in v0.1.3
Render renders comp into buffers and returns the buffered output.
Render is the recommended entry point for HTTP handlers. It gives the same parallel rendering behavior as NewPrinter: fragments marked with ~>(goxx.Parallel()) are scheduled on the worker pool, and their buffered output is kept in template order. Rendering finishes before anything is written to the http.ResponseWriter. If rendering succeeds, set headers or a custom success status and then call WriteTo. If rendering fails, no output is returned, so the handler can still send an error response.
Check context.Canceled and context.DeadlineExceeded separately: they mean the render context ended before rendering finished. If Render succeeds but WriteTo fails, that error came from the final writer.
Example (HttpHandler) ¶
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"github.com/doors-dev/gox"
"github.com/doors-dev/goxx"
)
func main() {
page := gox.Elem(func(cur gox.Cursor) error {
return cur.Text("ok")
})
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
out, err := goxx.Render(r.Context(), page)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
slog.Debug("render stopped before completion", "err", err)
return
}
if err != nil {
http.Error(w, "render failed", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
if _, err := out.WriteTo(w); err != nil {
slog.Warn("response write failed", "err", err)
}
})
_ = handler
}
Output: