gox

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: 0BSD Imports: 38 Imported by: 2 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

GoX

Go reference

Typed HTML templates written in real Go. Write ordinary Go functions with variables, loops, branches, and closures, and drop HTML markup right where you want output. Components are just functions, their inputs are type-checked at compile time, and every value is escaped automatically for the exact HTML, attribute, URL, JavaScript, or CSS context where it lands.

GoX compiles and runs on EVM. You can compile templates once, link them to your Go application's APIs, and recompile them at run time without rebuilding or restarting your server. It requires no database and no KVO models.

A 60-second tour

Here is a complete GoX package. Updates is the entry point: the host passes viewer and accent, and GoX supplies the *gox.Builder automatically. Like ordinary Go, every .gox file imports the packages it uses.

package updates

import (
	"fmt"
	"strings"

	"github.com/andreyvit/evm/gox"
)

type Release struct {
	Name string
	Done bool
}

func Updates(b *gox.Builder, viewer string, accent string) {
	items := []Release{{"Live templates", true}, {"Native builds", false}}
	status := func(done bool) string {
		if done {
			return "shipped"
		}
		return "on the roadmap"
	}
	<Panel title="What shipped">
		<slot-body>
			<p>
				"Hello, "
				viewer
				"!"
			</p>
			<List items={items}>
				<slot-row>
					<strong>item.Name | strings.ToUpper</strong>
					" "
					<Badge label={status(item.Done)} />
				</slot-row>
			</List>
		</slot-body>
		<slot-after>
			fmt.Sprintf("%d updates. No host rebuild.", len(items))
		</slot-after>
	</Panel>
	<style>.badge { color: {=accent}; }</style>
	<script>window.viewer = {=viewer};</script>
}

func Panel(b *gox.Builder, title string, body func(), after func()) {
	<section>
		<h1>title</h1>
		body()
		<footer>after()</footer>
	</section>
}

func List(b *gox.Builder, items []Release, row func(item Release)) {
	<ul>
		for _, item := range items {
			<li>row(item)</li>
		}
	</ul>
}

//gox:arg accent implicit
func Badge(b *gox.Builder, label string, accent string) {
	<span class="badge" title="Accent: {accent}">label</span>
}

Five things to notice right away:

  • Components are just Go functions. <Panel>, <List items={items}>, and <Badge /> are type-checked function calls—attributes map to named parameters, not untyped string maps.
  • Slots are typed callbacks. row func(item Release) passes each item into <slot-row>, while the slot still closes over status and accent from the caller. Named slots let Panel place its body and footer wherever it likes.
  • Explicit data, implicit context. List takes items explicitly, while Badge picks up accent from the caller's scope via //gox:arg accent implicit. Every component receives *gox.Builder automatically.
  • Real Go everywhere. Structs, slices, closures, if, for, fmt.Sprintf, and formatting pipelines (item.Name | strings.ToUpper) live right alongside markup.
  • Automatic contextual escaping. When viewer is <Ada & Bob>, the greeting emits &lt;Ada &amp; Bob&gt; while <script> receives a safely quoted JavaScript string. Attributes and CSS use their own rules—no manual escaping needed.

The runnable version of this example lives at testdata/examples/showcase.gox, and showcase_test.go compiles, links, renders, formats, and live-recompiles it in a single process.

Live recompilation—and what GoX is not

Because GoX embeds EVM's compiler, your server can accept edited template source, compile and link a new *gox.Program, and atomically swap it in while in-flight requests finish on the old program. You never rebuild or restart the host binary (your host Go APIs stay compiled into the binary). File watching and deployment policy stay in your application—GoX does not run a background reload daemon.

Two important boundaries to know up front:

  • Server-side only. GoX emits HTML, text, or CSS on the server. It has no client-side runtime, browser state, or hydration.
  • Native Go code generation is on the roadmap, not implemented yet. The native compilation proposal describes how EVM-in-dev + optional native-in-prod will share one frontend and manifest format. Today's GeneratedSources is inspectable compiler output for debugging, not standalone Go code.

Contents

Run your first template

In any Go module that depends on github.com/andreyvit/evm (using the Go version in go.mod), save this as main.go and run go run .:

package main

import (
	"context"
	"fmt"

	"github.com/andreyvit/evm/gox"
	"github.com/andreyvit/evm/values"
)

func main() {
	tmpl := must(gox.Compile("greeting.gox", `package greeting

import "github.com/andreyvit/evm/gox"

func Greeting(b *gox.Builder, name string) {
		<p>
			"Hello, "
			name
			"!"
		</p>
	}`))
	program := must(tmpl.Link())
	html := must(program.Render(context.Background(), []values.Value{
		values.String("Alice & Bob"),
	}, gox.RenderOptions{Strict: true}))
	fmt.Println(html)
}

func must[T any](value T, err error) T {
	if err != nil {
		panic(err)
	}
	return value
}

Output:

<p>Hello, Alice &amp; Bob!</p>

Notice that you pass only name; GoX supplies the builder. Compile and link once at startup, then reuse program across concurrent requests.

For larger runnable examples, see example_test.go, examples/gox-basic (multi-file templates with layouts, slots, implicit arguments, pipelines, and live recompilation), and the loyalty example with typed KVO models.

Syntax

GoX is Go with HTML tags. Before Go parses the file, GoX lowers the markup into builder calls and then runs EVM's type checker and compiler. HTML, text, and CSS templates share the same frontend and differ only in their escaping context.

Keep these core rules in mind:

  • Tags start with < anywhere in a rendering function. Strings and Go comments protect their contents, and multiline opening tags continue until >.
  • Parenthesize < comparisons: Write if (age < minimumAge) { ... } and for i := 0; (i < count); i++ { ... }. Operators <=, <<, and <<= keep their normal Go meaning. Ordinary { re-enables markup (even inside a callback inside parentheses), and } restores the enclosing mode. Inside an attribute's {expression}, everything is pure Go.
  • Tags separate statements. <p>name<strong>"VIP"</strong></p> works on one line. Adjacent Go expressions still need a newline or semicolon: <p>"Hi, "; name</p>.
  • Quote literal text. Source indentation and line breaks emit nothing; spaces inside quoted strings are preserved. Use <p>name</p> for variables and <p>"{"</p> for a literal brace—a bare {a} in element body is a Go block, not interpolation.
  • JavaScript and CSS keep their own syntax. Insert Go data inside <script> and <style> with {=expression}.
  • Friendly field selectors. KVO selectors use model aliases, and Go struct fields also accept unambiguous JSON names and snake_case (AppIDs → app_ids, URLs → urls).
Fragments

When you want to emit output without wrapping it in an HTML element, use a fragment (<>...</>):

func Greeting(b *gox.Builder, name string) {
    <>
        "Hello, "
        name
        "!"
    </>
}

Fragments must balance inside each branch, loop body, and component. They do not introduce a Go variable scope (use a Go { ... } block if you want one). Inside an element, attribute helpers called through a fragment still apply to the element's pending opening tag. Fragments also work in text and CSS templates.

What produces output

Inside an element or fragment, standalone expressions render to the output; assignments and control flow do not. Outside markup, ordinary Go rules apply.

Inside an element or fragment What happens
"Hello" Renders literal text
customer.Name Renders a string or trusted content, escaped for its context
strconv.Itoa(customer.Points) Converts a number, then renders the string
order.Placed | app.FormatDate Calls app.FormatDate(order.Placed) and renders the result
label(customer) Renders the helper's single return value
Card(customer) Calls a renderer that writes through the builder
label := customer.Name Assigns a variable; emits nothing
_ = lookup() Calls a function and discards its result
if enabled { ... } Ordinary Go control flow

Functions returning no values run for their rendering or side effects; functions returning one value render that value. Multiple return values cannot be rendered as a single expression, and a function value must be called to render it.

func Names(b *gox.Builder, names []string) {
	<ul>
		for i, name := range names {
			<li>
				fmt.Sprintf("%d. %s", i+1, name)
			</li>
		}
	</ul>
}
Why text requires strings

Text positions—element bodies, fragments, <title>, <textarea>, and text templates—accept only strings (including named string types), trusted content types such as gox.HTML, and gox.Content. Passing an int or other type is a compile error:

page.gox:25:5: cannot output a value of type int: text takes strings and trusted content; convert it

This is deliberate: GoX never guesses how to format a value for a human reader. Even one integer type reads differently depending on context—a balance as 1,250, a delta as +1,250, a year as 2026, a count as 2,026. Convert in place (strconv.Itoa(n), strconv.FormatBool(ok), fmt.Sprintf(...)) or pipe through a domain formatter (price | app.FormatMoney).

Values by position

A simple rule of thumb explains every position's accepted types: text is for people, attributes/CSS/JS are for the browser. People need explicit formatting, so text requires strings. Browsers expect raw numbers and booleans in attributes, CSS, and JavaScript, so GoX accepts them directly there.

Position Example Accepted types
Text <p>name</p>, <title>, <textarea> Strings, trusted content, gox.Content
Whole attribute colspan={n}, hidden={flag} Strings, integers, bool
Interpolated attribute href="/items/{id}", title="{count} items" Strings, integers
CSS {=value} in <style>, style="width: {percent}%" Strings, integers, floats
JavaScript {=value} in <script>, onclick="go({id})" JSON-compatible values

For anything else, format it with a function or pipeline. Extra parameters on that function are filled using implicit arguments, so a helper like func FormatMoney(m Money, locale *Locale) string needs no explicit locale at the call site:

<td title="{order.Placed | app.FormatTime}" data-total={int64(order.Total)}>
	order.Total | app.FormatMoney
</td>

Type nuances to know:

  • Strings include named string types, trusted content types, and gox.Content.
  • Integers in attributes cover int, int8, int16, int32, int64, uint, uint16, uint32, and uint64. byte, uint8, and rune are rejected as ambiguous (character vs. number—convert to string or int), and uintptr is rejected too. CSS accepts all of them as numbers.
  • Named numeric types (like type Money int64) are not treated as plain integers, because raw cents are rarely what a browser or reader expects. Convert explicitly (int64(price)) or call a formatter.
  • Floats are allowed in CSS (always decimal, never scientific notation) and JavaScript.
  • Booleans are accepted as whole attribute values only.
  • Component arguments, b.Attr, b.Text, and pipelines keep their normal Go types.

Attributes and escaping

HTML attributes come in three flavors: quoted static text (class="card"), a whole dynamic value (aria-disabled={disabled}), or interpolation inside a quoted string (href="/customers/{id}"):

func Link(b *gox.Builder, id string, label string, disabled bool) {
	<a href="/customers/{id}" aria-disabled={disabled}>
		label
	</a>
}

Boolean attributes follow HTML semantics:

  • Standard boolean HTML attributes (disabled, hidden, checked, etc.) are omitted when false and emitted when true.
  • All other attributes (aria-disabled, aria-expanded, contenteditable, draggable, spellcheck, data-*) print "true" or "false", because those attributes distinguish "false" from absent.

Component attributes are typed Go function arguments, not HTML attributes.

Special attribute contexts:

  • style attributes are CSS (strings, integers, floats), just like {=value} in <style>.
  • Event-handler attributes (onclick, etc.) are JavaScript and accept JSON-compatible values (booleans, numbers, strings, time.Time, and slices, arrays, or maps of those). Passing a struct compiles and fails at render time—pass a map or string instead.

Escaping is always contextual. Text, attributes, URLs, srcsets, JavaScript, and CSS each get their own escaping and filtering rules (the same rules as Go's html/template). Put raw data in expressions—never pre-escape it. Reserve trusted-content types like gox.HTML strictly for markup your application already trusts (see Safety and failures).

Components

Every component is an ordinary Go function taking *gox.Builder as its first parameter. Call it with XML syntax (<Button label="Save" />) or Go syntax (Button("Save")), or pass it around as a function value (chosen := Button, <chosen label="Save" />).

  • Tags resolve exact, case-sensitive Go names, including package selectors (<ui.Button>) and method values (<card.Render>).
  • Component attributes match parameter names verbatim, including camelCase.
  • A callable in scope takes precedence over a same-named HTML tag—capitalize component names to avoid collisions. Unknown capitalized tags are compile errors.
  • Every renderer declares its full Go signature (anonymous parameter-list templates are not supported). See the tested examples in page.gox and button.gox.
Slots are typed callbacks

A component accepts child markup as one or more func(...) parameters and decides where—and how many times—to invoke them:

func Panel(b *gox.Builder, title string, body func(), after func()) {
	<section>
		<h2>title</h2>
		body()
		<footer>
			after()
		</footer>
	</section>
}

func Page(b *gox.Builder, name string) {
	<Panel title="Profile">
		<slot-body>
			<p>name</p>
		</slot-body>
		<slot-after>
			<a href="/profiles">"All profiles"</a>
		</slot-after>
	</Panel>
}
  • Children placed directly inside <Panel>...</Panel> without a <slot-...> wrapper fill the body slot.
  • Parameterized slots like row func(item *Item) bring item into scope inside <slot-row>, while keeping access to all of the caller's local variables.
  • Because a slot is a real callback, the component can render it conditionally, skip it, or call it inside a loop. Duplicate or unknown slot/attribute names are compile errors.
Local components

You can define helper components inline as Go closures and call them with either Go or XML syntax:

item := func(label string) {
    <strong>
        page.Title
        ": "
        label
    </strong>
}
<div>
    item("one")
    <item label="two" />
</div>
Implicit arguments

To avoid threading request-wide context (like a theme, store, or viewer) through every component call, mark a parameter implicit—either per function with //gox:arg name implicit or globally by exact full type path via gox.Options{ImplicitTypes: []string{"*bh/model.ViewData"}}.

*gox.Builder and *evmruntime.Ambient are always implicit. No other parameter ever inherits implicitly just because a local variable shares its name.

//gox:arg active implicit
func Badge(b *gox.Builder, label string, active bool) {
	<span class="badge" aria-disabled={!active}>
		label
	</span>
}

func Page(b *gox.Builder, active bool) {
	<div>
		Badge("Member")
		<Badge label="Guest" active={false} />
	</div>
}

Here, Badge("Member") lowers to Badge(b, "Member", active), while <Badge label="Guest" active={false} /> passes false explicitly—an explicit argument always wins.

In positional Go calls inside .gox files, GoX binds supplied arguments in order and inserts eligible implicit parameters whenever the next supplied argument does not match (taking the builder from the enclosing renderer and other implicit values from a same-named, assignable variable in lexical scope). Optional omitted parameters receive zero values. Ordinary .go files keep standard Go call rules.

Rendering functions containing markup must return no values. Helper functions without top-level markup can return values (including gox.HTML); calling one in output position renders its return value (use _ = helper() to discard it).

XML parameter conventions

Beyond ordinary named attributes, XML component calls support prefixed map inputs, ambient overrides, optional parameters, and assignment.

Prefixed map inputs and ambient overrides
<Card
    ambient:color="blue"
    someExtras:class="compact"
    otherExtras:{key}={value}
    enabled={false}
/>
  • ambient:key={value} requires a caller variable ambient and a callee parameter both of type *evmruntime.Ambient. GoX passes ambient.CloneWith(map[string]any{"key": value}), leaving the caller's ambient untouched while descendants inherit the updated one (keep the parameter named ambient to preserve name-based inheritance). ambient:{key}={value} works with a dynamic string key too.
  • someExtras:key={value} builds a fresh map for the map parameter named someExtras (there is no magic extras name—each prefix selects its own parameter, and maps never merge with a caller's map). Bare someExtras:enabled sets the value to true.
  • someExtras:{key}={value} uses a Go expression as the key. Typed and named maps work as long as key and value types match; static keys are strings (hyphens and extra colons stay part of the key).
  • You cannot mix someExtras={wholeMap} with someExtras:key=... on the same parameter in one call.

See testdata/examples/map-inputs.gox for a runnable example.

Nested map inputs

Add more colon-separated segments to build nested submaps:

<helpers.Subblock
    key="email-input"
    data={customer}
    extras:attrs:type="email"
    extras:attrs:name="email"
    extras:attrs:value={customer.Email}
    extras:attrs:placeholder="you@example.com"
/>

This populates extras["attrs"] as map[string]any (typed nested map parameters keep their declared map types). Computed leaf keys work (extras:attrs:{key}={value}), and you can quote keys containing colons (args:{"tier::class"}={className}). Mixing a whole submap with nested entries at the same path, duplicate constant keys, or treating a scalar as a submap are compile errors. Leaf key and value expressions are evaluated once.

Optional parameters and //gox:arg

Parameters are required by default—parameter names have no special optionality convention. Mark optional inputs with a //gox:arg directive right above the function:

//gox:arg label optional legacyName="String Arg Name"
func Card(b *gox.Builder, label string) { <p>label</p> }
func Page(b *gox.Builder) { <Card label="Hello" />; <Card /> }
  • //gox:arg supports optional / optional=false, implicit / implicit=false, and legacyName="...". Unknown parameters or options and duplicate legacy names are errors.
  • XML calls always use the Go parameter name (label); legacyName is for host adapters that pass arguments by string key (exposed via Program.ParametersFor(name), Template.Parameters, and Parameter.InputName()).
  • An optional *T parameter accepts a raw T in either XML or GoX call syntax (evaluated once and wrapped in a fresh pointer), or nil when omitted.
  • Use plain string with optional when empty and missing mean the same thing. Use gox.Optional[T] (Value T, Present bool) when you need to distinguish an explicit false or 0 from omission.
  • Structural optional types: Any named struct starting with Optional, Opt, Maybe, or Null that has a Value field and one bool field named Valid, Present, IsValid, or IsPresent (including generic instances like Optional[bool]) is recognized as optional automatically unless marked optional=false. Omitting it passes the zero value (or nil for a pointer); passing a raw value constructs the wrapper with the presence flag set to true.

Optional and implicit rules apply to both XML calls and ordinary calls in .gox files; prefixed map entries apply to XML calls.

Assigning XML call results

A self-closing XML call to a function that returns a value can be the right-hand side of := or =. Assignment returns the Go result without rendering it:

tierContent := <TierLabel name={tier.Name} />
content := <Translate
    key={key}
    args:{dimension}={value}
    args:{dimension + "::class"}={valueClass}
    args:tier={tierContent}
/>
<p>content</p>

Optional inputs, implicit context, and prefixed map inputs work normally here. Assignment supports self-closing calls only (not calls with slots or HTML fragments), and functions with no return values cannot be assigned.

Attribute helpers and pipelines
Imperative attribute helpers

When you want to share attribute logic across elements, write a helper taking *gox.Builder and call it right inside an opening tag before any child output:

func decorate(b *gox.Builder, disabled bool) {
    b.Class("button")
    b.Attr("aria-disabled", disabled)
    b.Attr("disabled", disabled)
}
  • b.Class("...") appends classes to the element's class attribute (empty strings are ignored).
  • b.Attr("class", "") clears classes; a nonempty b.Attr("class", ...) appends. For all other attributes, b.Attr replaces any earlier value.
  • b.AttrOrOmit("id", anchor) skips nil and empty strings (including named string types) while keeping normal Attr behavior for 0, false, and whitespace.
  • b.Data("action", "change->upload#select") sets data-action as escaped text without inferring URL or CSS filtering from the suffix (ideal for Stimulus actions and similar data attributes). The suffix must be a nonempty ASCII attribute name.
  • Void elements (<input>, <img>, with or without />) stay pending until the next output, so a helper right after <input type="text"> modifies that <input>, not its parent. Attribute helpers also work before content inside <textarea> and <title>:
<input type="text">
decorate(false)
<textarea>
    decorate(false)
    value
</textarea>
  • Calling an attribute helper after child output begins fails the render.
  • b.Open, b.Close, b.Text, and b.HTML let you emit markup imperatively from Go helpers. The builder is valid only during its render and checks context at run time so a callback can never inject HTML into a JS, CSS, or attribute context.
Formatting pipelines

Use | to chain formatting calls cleanly from left to right:

name | strings.ToUpper                  // strings.ToUpper(name)
name | strings.TrimPrefix(_, "Dr. ")    // strings.TrimPrefix(name, "Dr. ")
count | fmt.Sprintf("%d items", _)      // fmt.Sprintf("%d items", count)
name | strings.TrimSpace | strings.ToUpper
  • value | f calls f(value).
  • value | f(arg) passes value as the last explicit argument: f(arg, value).
  • Put _ where value should go when it is not last (one _ per call, as a whole argument—x | f(g(_)) is a compile error).
  • Implicit arguments still apply, and pipelines work anywhere in a .gox file (output, attributes, component arguments, assignments).
  • The right side must be a callable returning a single value. Integer bitwise OR keeps its normal Go meaning unless the right side uses _.
Building class strings with ClassList

Use b.Class when styling the current element. When you need to build a class string to pass into another component, use gox.ClassList:

var classes gox.ClassList
classes.Add("button")
classes.Add(extraClass)
if disabled {
    classes.Add("opacity-50")
}
<Button class={classes.String()} />

Add skips empty strings, trims surrounding whitespace, and joins groups with single spaces (without deduplicating or reordering). Its zero value is ready to use. Because it wraps strings.Builder, do not copy a ClassList after its first Add. String() does not copy the buffer, and later additions leave earlier strings untouched.

Plain text or trusted HTML (gox.Content)

When a component input (such as a heading or label) should accept either plain text or trusted HTML, type it as gox.Content:

func Title(b *gox.Builder, title gox.Content) { <h1>title</h1> }
// Callers pass:
//   title={gox.Text("<plain text>")}
//   title={gox.TrustedHTML(gox.HTML("<em>trusted markup</em>"))}

Content has two fields (Content string, TrustedHTML bool) and its zero value is empty plain text. Text escapes for the output context; TrustedHTML asserts that the caller already trusts the markup (it does not sanitize it, and attribute contexts still apply contextual escaping).

Inside EVM, Content and common Optional[T] specializations pack into a single EVM value without heap boxing. See compact value representations.

Ambient context

evmruntime.Ambient is an immutable, persistent map[string]any chain for inherited presentation choices (themes, density, overlay classes). Pass real domain data (list items, view models) explicitly or via ImplicitTypes, and keep extras and ambient parameters last, in that order.

  • A nil *evmruntime.Ambient is an empty context.
  • Get(key) walks from newest layer to parent; CloneWith(map[string]any{...}) creates a child layer without mutating the parent (an explicit nil hides a parent value; stored values are not deep-copied). Values() returns a flattened map copy.
  • Files naming the type import "evmruntime". Because *evmruntime.Ambient is built-in implicit, naming the parameter ambient lets calls inherit it automatically.
Binding an ambient key directly to a parameter

Use //gox:arg paramName ambient="key" to read an ambient key straight into a typed parameter:

//gox:arg mediaOverlayClass ambient="media_overlay_class"
func Card(b *gox.Builder, title string, mediaOverlayClass string, ambient *evmruntime.Ambient) {
    <article class={mediaOverlayClass}>title</article>
}

func Page(b *gox.Builder, ambient *evmruntime.Ambient) {
    <Card title="Preview" ambient:media_overlay_class="overlay" />
    Card("Another card")
}

Rules for ambient="...":

  • The function must declare exactly one *evmruntime.Ambient parameter.
  • The bound parameter cannot be passed explicitly in XML or GoX calls.
  • Missing or nil values yield the parameter's zero value; non-nil values must match its type. Reading does not mutate or remove the key.
  • ambient cannot be combined with optional, implicit, or legacyName. Host adapters can inspect Parameter.Ambient, and gox.AmbientSchema() exposes Ambient to host-bound helpers.

GoX follows the same three-stage lifecycle as EVM:

Stage Input Output
Compile .gox/.go source + package declarations Checked EVM module + rendering plan (*gox.Template)
Link *gox.Template + host implementations Immutable, concurrent *gox.Program
Render *gox.Program + ctx + ordered arguments Escaped output string or an error

Every .gox file starts with a package clause (after any frontmatter and leading comments) and imports every package it references: EVM built-ins (errors, fmt, log, math, strconv, strings), evmstd packages (time, slices, maps, net/url, regexp, etc., included automatically by GoX), github.com/andreyvit/evm/gox, and your host packages. Referencing a known package without importing it fails with a helpful message:

greeting.gox:3:18: undefined: gox (add import "github.com/andreyvit/evm/gox")

Run gox fmt or gox.FormatSource to add missing imports automatically.

Compile options and shared environments

gox.Options has Compile, CompileSources, and CompileFiles methods matching the package-level functions:

Option Purpose
Format Output language: HTMLFormat (default), TextFormat, or CSSFormat
ImplicitTypes Full type paths whose parameters are implicit
ParseFrontmatter Parser callback for file frontmatter
AllowMissingPackage Accept legacy files without a package clause
Environment Pre-checked package environment (replaces passing packages on every call)

If your host compiles or links many templates against the same host packages, prepare them once:

  • env := gox.NewEnvironment(packages...) type-checks your host packages, the GoX runtime, and evmstd once. Pass Options{Environment: env} to compile, preprocess, or format without repeating the package list. env is immutable and safe for concurrent use, and env.Compiler() compiles plain Go against the same packages.
  • links := env.NewLinkEnvironment(packages...) validates and binds host packages once, so links.Link(tmpl) and links.LinkModule(module) keep only what each template needs while sharing host shapes and native method tables. (Use gox.LinkOptions{DefaultLimits: &limits, Compilers: ...}.NewEnvironment(packages...) to configure program default limits or share a separate evmcompiler.Environment, or gox.NewLinkEnvironment(packages...) without a compile environment.)

To expose a Go package from your app, bind it with evmbind.New and pass schema.Package() to Compile and schema.Bind(nil) to Link (or the bound package to both).

Rendering
  • program.RenderNative(ctx, []any{user, cart, query}, opts) passes ordinary Go values (scalars, time.Time, pointers to bound native structs, and bound slices/maps). Types must match parameters exactly—no implicit numeric conversions. Use RenderFuncNative(ctx, "Card", args, opts) to pick a renderer by name.
  • program.Render / program.RenderFunc accept values.Value arguments, including KVO records and script structs. Inspect Template.Parameters for entry argument order.
  • A linked *gox.Program renders concurrently: each render acquires a pooled *evm.VM (program.AcquireVM(ctx); defer vm.Release()) and a pooled builder, starting with fresh package globals and the program's default limits. A non-empty RenderOptions.Budget overrides the VM's budget via vm.SetBudget, ctx cancels execution, and output is capped at 16 MiB by default (MaxOutputBytes overrides it). Failed renders return an error and never emit partial output.

Files and frontmatter

  • CompileSources(files, packages...) compiles a map[string]string of .gox and .go files with no default entry (call RenderFunc / RenderFuncNative by name).
  • CompileFiles(entry, files, packages...) also selects the default renderer by function name or .gox filename (picking its first renderer).
  • Imports are file-local, and all files in one compilation must declare the same package name (unless AllowMissingPackage is set for legacy sources, which default to plugin).

A .gox file may begin with a --- frontmatter block:

---
title: "Rewards"
---

package rewards

import "github.com/andreyvit/evm/gox"

func Render(b *gox.Builder) {
    <h1>"Rewards"</h1>
}

Set Options.ParseFrontmatter to func([]byte) (any, error) using whatever metadata format you prefer (YAML, JSON, NAML, etc.). GoX calls it during preprocessing with the bytes between the --- lines. Frontmatter without a parser—or a parser error—fails compilation with the filename; files without frontmatter need no parser.

Metadata attaches to every named top-level function in that file (use separate files for different metadata). Inspect files and metadata via:

  • Template.Files(), Template.File(name), Template.FunctionFile(fnName) (and the same methods on Program). Each gox.File holds Filename, Name (basename without .gox), Functions (declaration order, excluding methods, init, _, anonymous functions, and .go files), Frontmatter, and HasFrontmatter.
  • Template.Frontmatter(fnName) / Program.Frontmatter(fnName) return (value, present) (staying present == true even if the parser returned nil). Treat parsed metadata as immutable; it lives on the template/program, not inside a serialized EVM module.
  • Options.CheckSyntax validates frontmatter delimiters; FormatSource preserves frontmatter bytes verbatim without needing a parser.

Whitespace and output formats

Source indentation and newlines between statements emit nothing—put spaces and newlines inside quoted strings (raw strings work nicely inside <pre>):

<pre>
    "first\n  second\n"
</pre>

Inline <script> and <style> bodies preserve their source whitespace and insert Go values with {=value}:

<script>const customer = {=customer};</script>
<style>.button { color: {=color}; }</style>

Rendered HTML has no automatic pretty-printer (pretty-printing can change inline spacing or <pre> semantics).

For non-HTML output, set Options.Format:

  • gox.TextFormat renders top-level expressions as plain text (following the string/trusted-content text rule).
  • gox.CSSFormat treats literal strings as CSS source and dynamic expressions as contextually escaped CSS values.

Both share the same Go frontend, type checking, nil guards, and EVM compiler; HTML markup tags require HTMLFormat.

Safety and failures

GoX's contextual analyzer and escaping functions come directly from Go's html/template (see upstream notes). At compile time, GoX determines the exact escaping filter for every expression; at run time, it calls those filters directly without ever running a Go text template.

  • Trust boundary: Template source and host packages are trusted code; input data is untrusted. Converting untrusted data to gox.HTML, CSS, JS, JSStr, URL, Srcset, or HTMLAttr bypasses escaping.
  • Structural protection: Dynamic script/style types are rejected, runtime attribute helpers cannot alter those types or meta-refresh parsing, and every rendering call verifies its active context at run time.
  • Nil-pointer resilience: A nil pointer dereference in an output expression reports a Diagnostic (via RenderOptions.Report, defaulting to slog.Warn) and skips that expression or component call—omitting the whole attribute in attribute position, or emitting a context-safe empty/null token in inline JS/CSS so neighboring tokens cannot merge unsafely. Set Strict: true to fail the render on nil diagnostics instead. Other EVM errors and host panics are never silently swallowed.
  • Balanced markup: Every branch, loop body, and component must contain complete elements. return, break, and continue automatically close any open elements they exit; labeled jumps across open elements are rejected.

Host integration

Calling guest renderers from Go
  • Standalone and delayed renders: Retain a renderer returned by a script with program.EVM().Callable(value) (or *evm.FunctionOf[func(*gox.Builder)]) and render it with program.RenderFunction(ctx, &fn, args, options). The callable keeps its original receiver/captures and owning *evm.Program alive across hot-reloaded versions. Each delayed render acquires a fresh VM lease (AcquireVM / Release) when it starts, getting fresh package globals and its own render budget while still seeing its captures. EVM does not synchronize concurrent writes to shared mutable captures—keep view captures immutable or synchronize access.
  • Rendering inside a host call: Use program.RenderFunctionInCall(vm, &fn, args, options) synchronously on the borrowed *evm.VM inside a native callback. Globals, captures, context, and remaining budget stay shared with the enclosing call (leave options.Budget zero), while nested renders get their own builder. Use vm.CallbackError or vm.PanicCallback in wrappers so guest panic recovery stays intact.
  • Storing renderers in Go structs: A native field of type *evm.FunctionOf[func(*gox.Builder)] holds a guest renderer closure or bound method that you can invoke with Program.RenderFunction.
Sharing GoX types with host packages
  • Pass gox.Schema() in evmbind.Options.Imports (keyed by GoX's import path) so host helpers can accept *gox.Builder and trusted-content types with the exact same type identities (Builder's private fields stay hidden). Do not retain or concurrently use a *gox.Builder outside its render.
  • gox.TrustedContentSchema() shares trusted-content types alone, and gox.AmbientSchema() shares *evmruntime.Ambient.
  • Function literals with their own named *gox.Builder parameter (not _) use that builder; literals without one capture the enclosing renderer's builder.
  • //gox:arg annotations also work on imported native functions bound via evmbind, and gox.PackageSource / gox.PackagesSource preserve them in printed declarations (via evmcompiler.DeclarationOptions.Annotations).
  • Additional helpers: gox.LinkModule(module, packages...) links a plain EVM module using *gox.Builder, gox.ProgramOf(evmProg) recovers the *gox.Program, gox.SplitFrontmatter splits a file without parsing metadata, and Template.FunctionParameters(name) inspects inputs before linking.

Class discovery

Template.Classes collects every static CSS class name (with source positions) from static attributes, constant interpolations, b.Class, b.Attr("class", ...), b.AttrOrOmit("class", ...), and ClassList.Add. Dynamic patterns like bg-{tone} are recorded in DynamicClasses rather than falsely emitting bg-, giving you exact input for a Tailwind safelist or host policy. (Classes synthesized inside opaque host helpers cannot be discovered statically.)

Execution coverage

Pass evm.NewCoverage(program.EVM()) in RenderOptions.Coverage to collect instruction counts and branch outcomes mapped back to original .gox source positions. Use one collector per concurrent run and merge afterward. Renders without this option collect nothing, even when reusing a pooled session; nested renders inside a native call inherit the enclosing VM's collector.

Source line coverage excludes compiler-generated nil guards and their fallback paths (raw instruction coverage still records them), while explicit template branches require both outcomes to be complete.

Source tooling

Editor highlighting is available for Zed, Vim, Neovim, VS Code, TextMate, Sublime Text, and Helix—see GoX editor support.

Install the CLI from this repository:

go install ./cmd/gox
gox check page.gox          # syntax check only
gox fmt page.gox            # print formatted source to stdout
gox fmt -w views/           # format .go and .gox files recursively
gox fmt -l views/           # list files that need formatting
cat page.gox | gox fmt      # read stdin (-filename aids import discovery)

gox check validates Go syntax and balanced tags without needing host packages (it rejects frontmatter because it has no frontmatter parser). gox fmt formats files, skips hidden directories and vendor, never overwrites invalid files, and accepts frontmatter verbatim. Both exit nonzero with file/line positions on error.

The same pipeline is available as Go APIs:

ensure(gox.CheckSyntax("page.gox", source))
formatted := must(gox.FormatSource("page.gox", source, hostPackages...))
lowered := must(gox.Preprocess("page.gox", formatted, hostPackages...))
fmt.Print(lowered.GeneratedSources["page.gox.go"])
tmpl := must(lowered.Compile())
// Link tmpl with matching host implementations, then render it.
Operation Needs host packages? What success guarantees
CheckSyntax / gox check No Go and GoX syntax and tag balance are valid
FormatSource / gox fmt Optional (for host import paths) Source is canonically formatted; unresolved names may remain
Preprocess Yes Typed lowering and contextual escaping analysis succeeded
Compile Yes Lowered Go compiles cleanly to EVM bytecode
Link Yes (bound implementations) All runtime bindings match and are ready to render

PreprocessSources and PreprocessFiles handle multi-file packages, and all three have Options variants for text and CSS formats. GeneratedSources is immutable diagnostic output referencing the separate rendering plan kept inside Preprocessed and Template.

All operations enforce nesting limits with source positions: up to MaxTagNesting (100) nested tags, plus evmcompiler's limits (32 type levels, 500 expression levels, 100 statement levels, 32 function-literal levels—note that a component with children compiles to a function literal—and MaxBranchWeight across the package).

Formatting rules

gox fmt is opinionated with zero style flags:

  • Go code follows gofmt with tabs; each paired tag or fragment indents one level, and required parentheses around < comparisons stay intact.
  • A single body expression or child element stays on one line with its tags if the whole line fits in 80 columns (counting each tab as 4 columns); anything longer splits across lines. Multiple body items always use separate lines.
  • Multiline opening tags put one attribute per line (indented one level) with > or /> aligned with <, keeping attribute order unchanged. Single-line self-closing tags include a space before />.
  • String literals, raw strings, and <script> / <style> bodies preserve their contents and whitespace untouched.
  • Imports are organized with goimports (adding missing imports and pruning unused ones). GoX runtime and evmstd paths are built in; host paths come from packages passed to FormatSource (unresolved packages stay intact).
func Card(b *gox.Builder, name string) {
	<section>
		<h2>name</h2>
		<Button
			label="An example with enough text to keep this opening tag multiline"
			name={name}
		/>
	</section>
}

Troubleshooting

Symptom What to check
Text is treated as an identifier Quote literal text: <p>"Hello"</p>
A < comparison parses as a tag Parenthesize it: if (a < b) { ... }
An argument is not inherited Only implicit parameters inherit. Check //gox:arg name implicit or ImplicitTypes, plus the caller variable's exact name, scope, and type
Unknown component error Match the exact case-sensitive Go name and ensure its package is imported
tags nest more than 100 levels / statements nest more than 100 levels Extract the inner markup into a helper component or function
function is too large / functions are too large together Split huge branching functions into smaller functions (or split the package)
undefined: x (add import "...") Add the missing import, or run gox fmt / FormatSource
cannot output a value of type int Text accepts strings and trusted content only—convert (strconv.Itoa(n), fmt.Sprint(v)) or pipe (price | app.FormatMoney)
cannot use a value of type ... as an attribute value Whole attributes take strings, bool, or integers—convert or call a formatter
cannot interpolate a value of type ... into an attribute Interpolated attributes take strings and integers—convert or pipe ({price | app.FormatMoney})
cannot use a value of type ... in CSS CSS takes strings, integers, and floats
Spaces between words vanish Source indentation emits nothing; put spaces inside quoted strings
Attribute helper fails at run time Call attribute helpers before the element emits any child output
Branch leaves an element open Keep complete, balanced elements inside every branch and loop body
gox check passes, Compile fails check is syntax-only and does not resolve imports, types, escaping contexts, or EVM limits
GeneratedSources does not compile as standalone Go It depends on the separate rendering plan stored in Preprocessed and Template
A value silently disappears Check nil-pointer diagnostics or render with Strict: true

When debugging a tricky template, work step by step: CheckSyntax → Preprocess (inspect GeneratedSources to see lowered calls and implicit arguments) → Compile → Link → render with Strict: true. Keep original filenames so every error points to the exact source line.

Tests and benchmarks

go test ./...
go test -race ./...
go test ./gox -run '^$' -fuzz FuzzCompileGoX -fuzztime=15s
go test ./gox -run '^$' -bench BenchmarkApplication -benchmem -benchtime=200ms -count=8

The application benchmarks test three anonymized, production-derived template suites (cards, history tables, and plan comparison matrices), each at two sizes, verifying byte-for-byte output parity with cached html/template. Compilation, linking, and view-model setup are outside the timer.

On Apple M5 Max with Go 1.27.3, GoX rendered all six workloads in 27–40% less time than cached html/template (eight samples, 2026-11-06). See the report for allocation counts, before-and-after samples, EVM controls, and reproduction commands. BenchmarkRender remains as a small diagnostic workload.

Documentation

Overview

Package gox compiles GoX templates into typed EVM functions.

Index

Examples

Constants

View Source
const MaxTagNesting = 100

MaxTagNesting bounds elements inside elements. The formatter indents one level per element, so its output grows with the square of the depth, and a component with children compiles to a function literal per level.

Variables

This section is empty.

Functions

func AmbientSchema

func AmbientSchema() *evmbind.Schema

AmbientSchema shares native inherited context with host rendering adapters.

func BuilderType

func BuilderType() *values.Type

BuilderType returns the guest type for the renderer's builder parameter.

func CheckSyntax

func CheckSyntax(filename, source string) error

CheckSyntax checks Go and markup syntax without resolving names or types. Compilation still checks call signatures, rendering contexts and EVM support.

func FormatSource

func FormatSource(filename, source string, packages ...evm.Package) (string, error)

FormatSource formats GoX and organizes imports, adding the missing ones. Packages supply virtual import paths; no host implementations or type checking are needed. Unknown packages remain unresolved. Layout is fixed, with an 80-column merge/split threshold.

func HTMLType

func HTMLType() *values.Type

HTMLType returns the guest type of trusted HTML.

func Package

func Package() evm.Package

Package declares and binds the rendering API for mixed EVM packages.

func PackageSource

func PackageSource(path string, packages ...evm.Package) (string, error)

PackageSource prints declarations with GoX argument annotations.

func PackagesSource

func PackagesSource(packages ...evm.Package) (string, error)

PackagesSource prints all exposed packages with GoX annotations.

func Schema

func Schema() *evmbind.Schema

Schema shares GoX's native builder and trusted-content types with host bindings. Builder fields remain private; the runtime package supplies its render methods.

func SplitFrontmatter

func SplitFrontmatter(filename, source string) (data []byte, body string, err error)

SplitFrontmatter extracts an optional leading --- block without interpreting its contents. A nil result means absent; an empty non-nil result means present. Body excludes the delimiters. Delimiters must occupy whole lines.

func TrustedContentSchema

func TrustedContentSchema() *evmbind.Schema

TrustedContentSchema shares GoX's supported html/template content types with host bindings: HTML, HTMLAttr, CSS, JS, JSStr, URL and Srcset.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is the render-scoped capability supplied implicitly to GoX helpers. Its opening tag remains mutable until the first child output. Do not retain it after a render or use it concurrently.

func (*Builder) Attr

func (b *Builder) Attr(name string, value any) error

Attr adds a named attribute to the current opening tag.

func (*Builder) AttrOrOmit

func (b *Builder) AttrOrOmit(name string, value any) error

AttrOrOmit sets an attribute unless value is nil or an empty string. Named string types retain Attr's escaping and trusted-content handling. An empty value does nothing, preserving any existing attribute.

func (*Builder) Class

func (b *Builder) Class(value string) error

Class adds a class to the current opening tag.

func (*Builder) Close

func (b *Builder) Close() error

Close ends the current element. It returns an error if there is no open element.

func (*Builder) Data

func (b *Builder) Data(name, value string) error

Data sets a data-* attribute as escaped text, without URL or CSS filtering. name is the suffix, for example "action" for data-action.

func (*Builder) HTML

func (b *Builder) HTML(value HTML) error

HTML writes trusted HTML in an HTML text context.

func (*Builder) Open

func (b *Builder) Open(name string) error

Open and Close support rendering helpers written against the builder directly. Raw script/style bodies need the compiler's contextual analysis or trusted HTML.

func (*Builder) Text

func (b *Builder) Text(value string) error

Text writes plain text, escaping it in HTML output.

type CSS

type CSS = template.CSS

CSS marks trusted CSS fragment for context-sensitive rendering. Construct it only from trusted input.

type Class

type Class struct {
	Name     string
	Position evm.SourcePosition
}

Class identifies a statically known CSS class, including classes in helpers.

type ClassList

type ClassList struct {
	// contains filtered or unexported fields
}

ClassList builds a space-separated class attribute for passing to another renderer. Prefer Builder.Class when styling the current element directly. The zero value is ready to use. Do not copy a ClassList after the first Add.

func (*ClassList) Add

func (c *ClassList) Add(group string)

Add appends a nonempty class group, separated by a single space. It trims surrounding whitespace without changing whitespace within the group.

func (*ClassList) String

func (c *ClassList) String() string

String returns the accumulated classes. Later Add calls leave it unchanged.

type Content

type Content struct {
	Content     string
	TrustedHTML bool
}

Content holds either plain text or explicitly trusted HTML. Its zero value is empty. TrustedHTML is a trust assertion, not a request to sanitize the content.

func Text

func Text(value string) Content

Text constructs plain content, escaped for its rendering context.

func TrustedHTML

func TrustedHTML(value HTML) Content

TrustedHTML constructs content whose markup is trusted by the caller.

type Diagnostic

type Diagnostic struct {
	Position evm.SourcePosition
	Message  string
}

Diagnostic identifies a GoX source error before rendering.

func (Diagnostic) Error

func (d Diagnostic) Error() string

Error formats the source filename, line, column, and diagnostic.

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

Environment is a set of host packages prepared for compiling many templates. It holds what every template can import: the supplied packages, the GoX runtime and the evmstd standard packages.

Building one type-checks all of those packages once. Compiles that name it in Options.Environment check only their own sources. An Environment never changes and is safe for concurrent use.

func NewEnvironment

func NewEnvironment(packages ...evm.Package) (*Environment, error)

NewEnvironment prepares the packages. It fails if any package cannot be imported, including one that no template would import.

func (*Environment) Compiler

func (e *Environment) Compiler() *evmcompiler.Environment

Compiler returns the prepared packages for compiling ordinary Go with evmcompiler. Its sources can import everything a template can.

func (*Environment) NewLinkEnvironment

func (e *Environment) NewLinkEnvironment(packages ...evm.Package) (*LinkEnvironment, error)

NewLinkEnvironment prepares packages for linking what this environment compiles. The packages declare what the environment was built from and bind the implementations, which a compile does not need.

type File

type File struct {
	Filename       string // Source map key, including directories and .gox extension.
	Name           string // Basename without .gox, suitable for host resource naming.
	Functions      []string
	Frontmatter    any // Host-parsed value; treat as immutable.
	HasFrontmatter bool
}

File describes a GoX source file. Functions lists its named package functions in declaration order (excluding init, blank names and methods). Function literals have no package name and are not included. Files need not have frontmatter or functions.

type Format

type Format uint8

Format selects output analysis at compile time.

const (
	HTMLFormat Format = iota
	TextFormat
	CSSFormat
)

Output formats select HTML context analysis, plain text, or CSS.

type HTML

type HTML = template.HTML

Trusted content types bypass selected escaping steps. Only construct them from trusted source, never by casting unvalidated input.

type HTMLAttr

type HTMLAttr = template.HTMLAttr

HTMLAttr marks trusted HTML attribute for context-sensitive rendering. Construct it only from trusted input.

type JS

type JS = template.JS

JS marks trusted JavaScript expression for context-sensitive rendering. Construct it only from trusted input.

type JSStr

type JSStr = template.JSStr

JSStr marks trusted JavaScript string content for context-sensitive rendering. Construct it only from trusted input.

type LinkEnvironment

type LinkEnvironment struct {
	// contains filtered or unexported fields
}

LinkEnvironment is a set of bound host packages prepared for linking many templates and modules. It adds the GoX runtime and the evmstd standard packages, as Template.Link does.

Building one validates and binds the packages once. Its programs share what the packages alone determine. A LinkEnvironment never changes and is safe for concurrent use.

func NewLinkEnvironment

func NewLinkEnvironment(packages ...evm.Package) (*LinkEnvironment, error)

NewLinkEnvironment prepares the packages for linking. It fails where every link against them would fail.

func (*LinkEnvironment) EVM

EVM returns the prepared packages for linking with package evm.

func (l *LinkEnvironment) Link(t *Template) (*Program, error)

Link binds a template to the prepared packages; see Template.Link.

func (*LinkEnvironment) LinkModule

func (l *LinkEnvironment) LinkModule(module *evm.Module) (*Program, error)

LinkModule links an ordinary Go module with GoX builder support; see LinkModule.

type LinkOptions

type LinkOptions struct {
	DefaultLimits *evm.Limits
	// Compilers are the compile environments that produce the templates and
	// modules to link. Programs of what they compile also share the linked
	// form of the host storage shapes. Anything else links the same way,
	// with shapes of its own.
	Compilers []*evmcompiler.Environment
}

LinkOptions configures a link environment.

func (LinkOptions) NewEnvironment

func (o LinkOptions) NewEnvironment(packages ...evm.Package) (*LinkEnvironment, error)

NewEnvironment prepares the packages for linking; see NewLinkEnvironment.

type Optional

type Optional[T any] struct {
	Value   T
	Present bool
}

Optional distinguishes an omitted component argument from an explicit zero. XML calls construct Value and Present through structural optional recognition.

type Options

type Options struct {
	// AllowMissingPackage accepts legacy files without a package clause.
	// They inherit the package name from the other files, or use plugin.
	AllowMissingPackage bool
	// ParseFrontmatter interprets file metadata during preprocessing, before type checking.
	// Nil rejects files containing frontmatter.
	ParseFrontmatter func([]byte) (any, error)
	Format           Format
	ImplicitTypes    []string
	// Environment compiles against prepared packages instead of a package
	// list. Functions that take packages must then receive none.
	Environment *Environment
}

Options fixes the output language during compilation. The zero value is HTML.

func (Options) CheckSyntax

func (o Options) CheckSyntax(filename, source string) (err error)

CheckSyntax validates host frontmatter and Go/markup syntax without type checking.

func (Options) Compile

func (o Options) Compile(filename, source string, packages ...evm.Package) (*Template, error)

Compile compiles one GoX file with these options.

func (Options) CompileFiles

func (options Options) CompileFiles(entry string, sources map[string]string, packages ...evm.Package) (*Template, error)

CompileFiles compiles the package and selects its default renderer by function name or filename.

func (Options) CompileSources

func (options Options) CompileSources(sources map[string]string, packages ...evm.Package) (*Template, error)

CompileSources compiles Go and GoX files together with these options.

func (Options) FormatSource

func (o Options) FormatSource(filename, source string, packages ...evm.Package) (_ string, err error)

FormatSource formats source, accepting a missing package clause only in legacy mode. Frontmatter stays opaque even if Options supplies a parser.

func (Options) Preprocess

func (o Options) Preprocess(filename, source string, packages ...evm.Package) (*Preprocessed, error)

Preprocess lowers a single GoX file for inspection with these options.

func (Options) PreprocessFiles

func (o Options) PreprocessFiles(entry string, sources map[string]string, packages ...evm.Package) (*Preprocessed, error)

PreprocessFiles preprocesses the package and selects its default renderer.

func (Options) PreprocessSources

func (o Options) PreprocessSources(sources map[string]string, packages ...evm.Package) (*Preprocessed, error)

PreprocessSources lowers the supplied package sources for inspection.

type Parameter

type Parameter struct {
	Name, Type string
	Optional   bool
	Implicit   bool
	LegacyName string
	// Ambient names a read-only inherited value; callers cannot supply this input.
	Ambient string
}

Parameter describes an entry input in Render argument order. Parameter describes a Go input and its legacy argument adapter metadata. Optional is effective optionality, including structural optional types.

func (Parameter) InputName

func (p Parameter) InputName() string

InputName is the key used by legacy string-keyed argument adapters. XML component calls always use Name.

type Preprocessed

type Preprocessed struct {
	GeneratedSources map[string]string
	// contains filtered or unexported fields
}

Preprocessed holds lowered Go and the rendering plan needed to compile it. GeneratedSources is for inspection, not standalone native Go builds. Treat the result and its maps as immutable.

func Preprocess

func Preprocess(filename, source string, packages ...evm.Package) (*Preprocessed, error)

Preprocess lowers one GoX file with the supplied EVM package descriptions. It validates types without generating bytecode or linking host implementations.

func PreprocessFiles

func PreprocessFiles(entry string, sources map[string]string, packages ...evm.Package) (*Preprocessed, error)

PreprocessFiles selects a default renderer by function name or GoX filename.

func PreprocessSources

func PreprocessSources(sources map[string]string, packages ...evm.Package) (*Preprocessed, error)

PreprocessSources lowers a mixed Go/GoX package without selecting a render entry.

func (*Preprocessed) Compile

func (p *Preprocessed) Compile() (_ *Template, err error)

Compile validates the lowered Go and emits EVM bytecode, retaining the rendering plan.

type Program

type Program struct {
	// contains filtered or unexported fields
}

Program binds a template to host implementations and supports concurrent renders.

func LinkModule

func LinkModule(module *evm.Module, packages ...evm.Package) (*Program, error)

LinkModule links an ordinary Go module with GoX builder support. Modules compiled from markup must use their Template.Link to retain the operations.

func ProgramOf

func ProgramOf(p *evm.Program) *Program

ProgramOf returns the GoX runtime that owns this linked program, or nil for programs linked without GoX. Retained functions keep this runtime alive.

func (*Program) EVM

func (p *Program) EVM() *evm.Program

EVM returns the linked program for ordinary Go entry points in this package.

func (*Program) File

func (p *Program) File(filename string) (File, bool)

File looks up the file by its exact source-map filename.

func (*Program) Files

func (p *Program) Files() []File

Files returns GoX file descriptions in filename order.

func (*Program) Frontmatter

func (p *Program) Frontmatter(name string) (any, bool)

Frontmatter returns the declaring file's metadata for a named function.

func (*Program) FunctionFile

func (p *Program) FunctionFile(name string) (File, bool)

FunctionFile finds the GoX file declaring a named package function.

func (*Program) ParametersFor

func (p *Program) ParametersFor(name string) []Parameter

ParametersFor returns declared inputs, excluding the builder, for a named renderer.

func (*Program) Render

func (p *Program) Render(ctx context.Context, args []values.Value, options RenderOptions) (string, error)

Render executes with EVM values in Parameters order. Globals and temporary state reset between renders. Host package implementations retain their own state.

func (*Program) RenderFunc

func (p *Program) RenderFunc(ctx context.Context, name string, args []values.Value, options RenderOptions) (string, error)

RenderFunc executes a named renderer. args excludes its explicit builder parameter.

func (*Program) RenderFuncNative

func (p *Program) RenderFuncNative(ctx context.Context, name string, args []any, options RenderOptions) (string, error)

RenderFuncNative executes a named renderer with ordinary Go values as its arguments, excluding the builder: strings and other scalars, time values, pointers to bound native structs, and registered interface implementations. A Go slice or map passes when a linked schema binds its type, as a field or in a signature. Each value must have the Go type its parameter is bound to; there are no implicit numeric conversions. Native pointers and collections are shared with the template, not copied. Conversion counts against options.Budget.

func (*Program) RenderFunction

func (p *Program) RenderFunction(ctx context.Context, fn *evm.Callable, args []values.Value, options RenderOptions) (string, error)

RenderFunction executes a retained renderer, including a closure or bound method. Captures remain shared; the render VM and builder reset between calls. args excludes the renderer's explicit builder parameter.

func (*Program) RenderFunctionInCall

func (p *Program) RenderFunctionInCall(c *evm.VM, fn *evm.Callable, args []values.Value, options RenderOptions) (string, error)

RenderFunctionInCall renders within an active native call, sharing its VM globals, captures, context and budget. It supports nested renders with separate builders. options.Budget must be zero; the enclosing invocation owns limits.

func (*Program) RenderNative

func (p *Program) RenderNative(ctx context.Context, args []any, options RenderOptions) (string, error)

RenderNative executes the default renderer with ordinary Go values. See RenderFuncNative.

type RenderOptions

type RenderOptions struct {
	// Coverage collects execution for this linked program; nil disables it.
	// Rendering inside a native call inherits its collector.
	Coverage *evm.Coverage
	// Strict fails on nil field access in output instead of reporting and omitting it.
	Strict bool
	// Report receives recoverable output diagnostics. Nil logs through slog.
	Report func(Diagnostic)
	// Budget applies the same execution limits as an EVM script.
	Budget evm.Budget
	// MaxOutputBytes defaults to 16 MiB. A failed render returns no partial output.
	MaxOutputBytes int
}

RenderOptions controls execution limits, output size, and strict rendering.

type Srcset

type Srcset = template.Srcset

Srcset marks trusted image source set for context-sensitive rendering. Construct it only from trusted input.

type Template

type Template struct {
	Entry            string
	GeneratedSources map[string]string
	DynamicClasses   bool

	Module             *evm.Module
	Parameters         []Parameter
	FunctionParameters map[string][]Parameter
	Classes            []Class
	GeneratedSource    string
	// contains filtered or unexported fields
}

Template holds bytecode and its immutable rendering plan. Keep the plan with the module; the module alone does not contain the contextual escaping filters. Treat exported metadata as read-only after compilation.

func Compile

func Compile(filename, source string, packages ...evm.Package) (*Template, error)

Compile compiles an HTML GoX file using EVM's standard and supplied packages.

Example
package main

import (
	"context"
	"fmt"

	"github.com/andreyvit/evm/gox"
	"github.com/andreyvit/evm/values"
)

func main() {
	tmpl, err := gox.Compile("greeting.gox", `package plugin

import "github.com/andreyvit/evm/gox"

func Greeting(b *gox.Builder, name string) {
    <p>
        "Hello, "
        name
        "!"
    </p>
}`)
	if err != nil {
		panic(err)
	}
	program, err := tmpl.Link()
	if err != nil {
		panic(err)
	}
	output, err := program.Render(context.Background(), []values.Value{
		values.String("Alice & Bob"),
	}, gox.RenderOptions{Strict: true})
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}
Output:
<p>Hello, Alice &amp; Bob!</p>

func CompileFiles

func CompileFiles(entry string, sources map[string]string, packages ...evm.Package) (*Template, error)

CompileFiles compiles one package and selects the default renderer by function name or filename. Component tags use exact Go callable names.

func CompileSources

func CompileSources(sources map[string]string, packages ...evm.Package) (*Template, error)

CompileSources compiles ordinary Go and GoX files into one package. Each GoX renderer declares its name and builder parameter. Use RenderFunc to select one. Every file imports the packages it names, including this one.

func (*Template) File

func (t *Template) File(filename string) (File, bool)

File looks up a GoX file by its exact source-map filename.

func (*Template) Files

func (t *Template) Files() []File

Files returns all GoX files in filename order. Returned slices may be modified.

func (*Template) Frontmatter

func (t *Template) Frontmatter(name string) (any, bool)

Frontmatter returns the host-parsed metadata of the file declaring name. Presence is independent of the parser's value, which may itself be nil. Treat returned metadata as immutable.

func (*Template) FunctionFile

func (t *Template) FunctionFile(name string) (File, bool)

FunctionFile returns the GoX file declaring a named package function. Ordinary Go files and unknown function names return false.

func (t *Template) Link(packages ...evm.Package) (*Program, error)

Link binds the module to host packages with matching signatures. It does not run it.

type URL

type URL = template.URL

URL marks trusted URL for context-sensitive rendering. Construct it only from trusted input.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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