Datapages templates

From romshark/datapages · View interactive version

Datapages templates is an AI agent skill from the GitHub repository romshark/datapages. Write Templ templates for a Datapages app: the generated href and action helpers, their options, attribute syntax and Templ pitfalls. Read with the datastar skill.

How to use this skill

Skill file

Templates

Read datapages first for the build loop, hard rules and naming conventions.

A Templ component implements datapages.Component, so handlers can return it. Run templ generate to compile .templ files to _templ.go. Datapages does not run this command. Templ docs: https://templ.guide/llms.md

href: links

The generated href package has one function for each page, named after its page type. Query structs are named href.Query<PageType>. Zero-value fields are omitted from the URL.

<a href={ href.PageIndex() }>Home</a>
<a href={ href.PagePost(post.Slug) }>{ post.Title }</a>
<a href={ href.PageMessages(href.QueryPageMessages{Chat: chatID}) }>Messages</a>
<a href={ href.Asset("logo.svg") }>Logo</a>

The linter rejects a hardcoded root-relative, relative, query-only or empty href. It also rejects javascript:. Literal https://, mailto:, tel:, #frag and //cdn.example.com values are valid. Use href.External(url) for an external URL computed by the app.

Inside href={ ... }, use an href package call, string literal or constant. The linter rejects variables and other calls, including fmt.Sprintf and templ.SafeURL, because it cannot resolve them.

action: Datastar actions

An action helper is action.<Owner>.<Name>.<METHOD>(...). Owner is the page type or App. PageLogin.POSTSubmit becomes action.PageLogin.Submit.POST(). (*App).POSTSignOut becomes action.App.SignOut.POST(). The helper returns the complete @post('/...') expression. After datapages gen, read the generated datapagesgen/action package for exact signatures.

<button data-on:click={ action.PageLogin.Submit.POST() }>Submit</button>
<button data-on:click={ action.PagePost.SendMessage.POST(slug) }>Send</button>
<button data-on:click={ action.App.SignOut.POST() }>Sign out</button>

A page action can be used only in that page's template. The linter rejects action.PageA.X.POST() in page B. App actions can be used on any page. Put an action expression in a Datastar action attribute, not in an href.

Every function takes variadic modifiers. Never hand-write the options object.

| modifier | argument |

| -------- | -------- |

| action.WithContentType | action.ContentTypeJSON, action.ContentTypeForm |

| action.WithSelector | CSS selector of the form to send |

| action.WithFilterSignals | include and exclude regexes, exclude may be empty |

| action.WithHeaders | map[string]string |

| action.WithOpenWhenHidden | bool |

| action.WithPayload | raw JavaScript expression |

| action.WithRetry | action.RetryAuto, RetryError, RetryAlways, RetryNever |

| action.WithRetryInterval, WithRetryScaler, WithRetryMaxWaitMs, WithRetryMaxCount | numbers |

| action.WithRequestCancellation | action.RequestCancellationAuto, ...Cleanup, ...Disabled |

| action.WithRequestCancellationController | expression holding an AbortController |

| action.WithBefore, action.WithAfter | JavaScript prepended or appended, joined with "; " |

| action.WithOption | raw key and value for anything the helpers miss |

Each modifier returns action.Option. Collect conditional modifiers in an []action.Option and pass opts.... Use this alias instead of importing runtime/actionexpr.

Pass arguments in this order: path variables in route order, the query value from <METHOD>Query(...), then modifiers. The query type is unexported. Create it with the generated constructor.

<button data-on:click={ action.PageMessages.Read.POST(
	action.PageMessages.Read.POSTQuery(msg.ID),
) }>Mark read</button>

Forms

Use a Datastar submit action to keep Enter-to-submit and browser validation:

<form
	data-signals:email="''"
	data-signals:password="''"
	data-on:submit__prevent={ action.PageLogin.Submit.POST() }
>
	<input type="email" data-bind="email" required/>
	<input type="password" data-bind="password" required/>
	<button type="submit">Sign in</button>
</form>

data-bind="email" puts the signal name in the attribute value. HTML lowercases attribute names, so use this form for camelCase signals. For live validation, add data-on:input__debounce.300ms to an input and call a validation action on the same page.

An action sends signals by default. To send form controls or file data, use action.WithContentType(action.ContentTypeForm). It selects the closest form. Add action.WithSelector("#login") only when the action must select another form. Form content type does not send signals.

Syntax

Pitfalls

<!-- written by datapages v0.10.0 -->

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.