--- name: arandu-view description: Write or change a page, layout, template, HTML fragment or component usage in an Arandu (Go) application -- anything under resources/views. Use when the request is to "add a page", "change the layout", "make a form", "render a list", "style this", "load this part with YPY", or when a fragment, ypy-get, ypy-target, @include or a partial is involved. The templates are .kyse.go files compiled to Go, and the syntax, the escaping rules and the Content-Security-Policy constraints are not the ones any other template engine uses. Covers @extends, @section, @foreach, the typed data struct, page against fragment, escaped versus raw interpolation, and why an inline style or an Alpine attribute will not work. license: MIT --- # Writing a view ## When to use A page a person navigates to, a part of one that YPY replaces, a form, a component call, the layout. The action that renders it is `arandu-http`; a JSON answer is `arandu-api`. ## Before you start - `resources/views/notes/` holds the example's four screens -- a listing through the `DataTable` component, drawn from the result of a typed listing, a record with a named action's form, and the create and edit forms built from `Field`, `Textarea` and `Checkbox` -- and `resources/views/partials/notes_table.kyse.go` is the listing's table, the fragment its page includes and the controller answers alone. `resources/views/comments/` is the same table over a nested resource, with `partials/comments_table.kyse.go` for its fragment. - A view is Go: `resources/views/home.kyse.go` compiles, through `aru view:build`, to `storage/framework/views/home.go`, which is build output and is gitignored. An error points at the line you wrote. ## Contracts and imports | piece | contract | | --- | --- | | page | `resources/views//.kyse.go`: `//go:build kyse`, `@extends('layouts.app')`, and a data struct that embeds `view.Page` (`github.com/arandu-io/hesape/view`), asserted with `var _ view.Layout = XData{}` | | fragment | `resources/views/partials/.kyse.go`, no `@extends`; its page draws it with `@include('partials.')`, handing the page's data over | | chrome | `view.New(ctx, title)` in the controller: the title, the flash a rejected form left, the CSRF token the middleware issued | | what a write did | the action says it with `ctx.With(hhttp.StatusKey, "Note saved.")` before redirecting, and the layout's `components.Toast` in `#toaster` draws it on the page the browser lands on, once | | words | `.Trans("notes.new")` in a page, a key of `lang/en.json`; the provider builds the translator from `lang/` and `bootstrap/app.go` hands it to `WithTranslator`. A locale is `lang/.json` with the same keys | | a date | formatted by the controller in `APP_TIMEZONE`, the zone the provider hands it from `cfg.Framework.App.Timezone`: `shownAt` in `app/Http/Controllers/Controller.go`. Never formatted as the driver handed it back, which may be UTC or the server's zone; the database stores UTC, so a service stamps `support.Now().UTC()` | | a table of records | `components.DataTable(components.DataTableProps{ID: ..., URL: .URL, Result: .Result, PageData: .Page, Searchable: true, FullPager: true, Columns: ..., Rows: ...})`, handed the `*pagination.ListingResult` the service returned. The state, the page with its total, the count beside every facet value and the page sizes come out of `Result`; every control is an address the state builds. The table draws its own pager, so no `components.Pagination` and no link written by hand sits beside it | | a table's columns | `components.TableColumn{Key: "title", Sortable: true, Hideable: true}`: the key is the listing's public key, and a column the listing did not declare sortable or hideable is not one | | a table's rows | `components.TableRow{Key: id, Label: "Select note ...", Cells: ..., Actions: []components.MenuItem{{Label: "View", URL: ...}}}`; an entry that edits is drawn only for a row whose address the controller set, which it does when the policy allows it | | a bulk action | `BulkAction` with the address of an endpoint that exists and `BulkActions` buttons with a `Name` and `Value` each; without `BulkAction` the bar is not drawn | | a button for an action | drawn only when the controller passed its address, which it does when the policy allows the action (`NoteService.Abilities`, over `auth.Allows`) | | answering | `ctx.View("notes.index", data)` for the page, `ctx.Fragment(http.StatusOK, "partials.notes_table", data)` for the fragment | | component | `github.com/arandu-io/kyse/components`, imported, never copied: an exported function taking its props struct | | behaviour | `ui.js`, bound once on `document`, dispatching on `data-*` attributes; this project registers its own in `resources/js/custom.js` with `arandu.ui.define` and `arandu.ui.action` | The directives are a closed set: `@extends` `@section`/`@endsection` `@yield` `@include` `@if`/`@elseif`/`@else`/`@endif` `@foreach`/`@endforeach` `@forelse`/`@empty`/`@endforelse` `@for`/`@endfor` `@while`/`@endwhile` `@continue` `@break` `@go`/`@endgo` `@csrf` `@attributes`. `{{ }}` escapes, `{!! !!}` does not, `{{-- --}}` never reaches the page. ## Procedure 1. **Write the `.kyse.go`**, its data struct in `@go`: ```go //go:build kyse package views import "github.com/arandu-io/hesape/view" @go // InvoicesData is what the controller hands this page. type InvoicesData struct { view.Page Invoices []InvoiceRow } // InvoiceRow is one invoice, already formatted by the controller. type InvoiceRow struct{ Reference string } var _ view.Layout = InvoicesData{} @endgo @extends('layouts.app') @section('content') @foreach(.Invoices as invoice)

{{ invoice.Reference }}

@endforeach @endsection ``` The build tag keeps the compiler out of the file: Go reads the constraint and never parses the markup below the package clause. 2. **Decide page or fragment.** Every navigation -- a direct visit, a link the layout's `ypy-boost:inherited` turned into a YPY request, a history restore -- gets the whole document, and the answer never changes on `YPY-Boosted`, so a reload shows the same page. A fragment is what one element asks for on purpose, with `ypy-get` or `ypy-post` and an `ypy-target` naming the element it replaces; it lives in `partials/` and the page includes it. 3. **Answer it from the controller** with the fragment only when `ctx.IsYPY()` and `ctx.Targets(id)` names that element (the header carries `tag#id`), the page otherwise, and `Vary: YPY-Request, YPY-Target` on both. 4. **Build, compile, check:** `aru view:build` names the template's line, `go build ./...` the Go's, `aru doctor` the escaping's. A rejected form is not a fragment. The action returns the `validation.Errors`, the router sends the browser back to the form -- YPY follows that as a navigation -- and the page after it shows the messages through `view.New`. The sign-in screens of `go run github.com/arandu-io/ui@latest auth` follow the same rule: a rejected sign-in is a redirect back to its screen, and a password never comes back. ## Commands - `aru view:build`, which also builds the stylesheet with the pinned Tailwind - `aru make:module` writes the four screens of a resource; `aru make:mail` writes a mail's two views - `aru dev` rebuilds the views on every change - `go get github.com/arandu-io/kyse/components` ## Example The controller side of one address with two representations, as `NoteController.Index` writes it: ```go compile package example import ( "net/http" hhttp "github.com/arandu-io/hesape/http" "github.com/arandu-io/hesape/pagination" "github.com/arandu-io/hesape/view" views "/storage/framework/views/notes" ) // Listing answers the page to every navigation, and the table alone to the // one request that names the table's root as the element it replaces. The // result is what the service's listing returned; the table is drawn from it. func Listing(ctx *hhttp.Context, rows []views.NoteRow, result *pagination.ListingResult) error { data := views.NotesIndexData{ Page: view.New(ctx, "Notes"), Notes: rows, NewURL: ctx.URL("notes.create"), URL: ctx.URL("notes.index"), Result: result, } ctx.Response.Header().Add("Vary", "YPY-Request, YPY-Target") if ctx.IsYPY() && ctx.Targets(views.NotesTableID) { return ctx.Fragment(http.StatusOK, "partials.notes_table", data) } return ctx.View("notes.index", data) } ``` ## Do not - Hand a view a map: a typo in a key is a blank space on a page that answered 200. `aru doctor` fails it as `view-data-is-a-map`. - Draw `components.Pagination` beside a `DataTable`, build a table's address from `q`, `sort` or `page` written in the view, or order and filter the rows in hand: the table draws the one pager and takes every address from the listing's state, and the work happens in the query. - Convert a string to `template.HTML` to get it through `{!! !!}`: a value somebody typed has been through nothing, and that is stored cross-site scripting. `{!! !!}` takes a component call; `raw-output-is-not-a-component`. - Write an `x-` attribute, an `@click` or Alpine: it is not served, the pages run under `script-src 'self'` with no `unsafe-eval`, and `aru doctor` fails it as `view-keeps-state-in-the-browser`. - Interpolate into an attribute the browser runs (`on*`, `ypy-on*`): the compiler refuses it. Data travels in a `data-*` attribute. - Write `style="..."`: `style-src 'self'` drops it. Use a class. - Answer a fragment from a view outside `partials/` (`fragment-without-partial`), or decide by `YPY-Boosted`. - Add a JavaScript dependency, a `package.json` or a CDN. `resources/js/custom.js` is the one script, served as written; `tests/Unit/resources_test.go` fails on any other. ## Extending it Anything a page needs in Go goes in the custom block inside its `@go`, and survives regeneration of the screens. A new component belongs in the kyse library, not in the application; a project's own behaviour is a named action in `resources/js/custom.js`. ## Wiring A page needs nothing: the controller that renders it imports its compiled package. A directory of views that no controller imports -- the layouts, the partials -- is linked with a blank import in `bootstrap/app.go`, or its views are never registered and the first request says so. ## Acceptance test - A feature test that loads each page and sees what it draws. - For an address with a fragment, the matrix of `TestTheNotesTableIsAnsweredAloneOnlyToTheElementThatAsksForIt`: a direct visit, a boosted link, a history restore, a YPY request for another element and the target named without YPY all get the whole document; the element's own request gets the fragment alone; every answer carries the `Vary`. - For a page with no fragment, the shape of `TestTheCommentPagesAreWholeDocuments`. ## Limits `ctx.View` takes the view's name as a string, so a missing view compiles; it is `aru doctor`'s `view-does-not-exist` and the tests that find it. The escaping is guaranteed for what passes through `{{ }}` and the components; a value a controller turned into `template.HTML` is trusted from then on. ## Gates Run them all, in this order, as `AGENTS.md` lists them: ```sh export GOWORK=off aru model:build --check aru view:build gofmt -l $(find . -name '*.go' -not -path '*/testdata/*' -not -name '*.kyse.go') go vet ./... bash tests/test-layout-guard.sh go test -race ./... go build ./... aru doctor ```