Add a page
Add a page to an app: a folder under pages/ with a template that declares its values, an
access rule on its path, and a data provider that fills the values. The example is the info tab
of a user's card in the people example, at /users/<login>/info.
Steps
-
Make the page's folder. Its path under
pages/is the page's address. A folder nameds_<name>captures one part of the address as a string, andn_<name>a whole number, sopages/users/s_login/info/is/users/<login>/info, such as/users/alice/info. Each folder is a Go package: name it with ASCII letters, digits and_, starting with a letter, and notmain,testdataor a Go keyword (E-GEN-043). -
Write the template,
index.html. Declare every value it shows with<ssr:var>and a Go type, and use the values in{{ }},ssr:ifandssr:for:<ssr:var name="user" type="User"/> <ssr:var name="lastSeen" type="string" reactive="true"/> <h2>Age group: <span id="age-group" ssr:if="user.Age <= 18">0-18</span> <span id="age-group" ssr:else-if="user.Age <= 30">19-30</span> <!-- the older groups --> <span id="age-group" ssr:else-if="user.Age <= 60">31-60</span> <span id="age-group" ssr:else>60+</span> </h2> <dl> <dt>Team</dt> <dd id="team">{{ user.Team }}</dd> <dt>Languages</dt> <dd id="languages">{{ user.Languages == '' ? 'none given' : user.Languages }}</dd> <dt>Works</dt> <dd id="works"> <span ssr:if="user.OnSite && user.Remote">on site and remotely</span> <span ssr:else-if="user.Remote">remotely</span> <span ssr:else>on site</span> </dd> <dt>Shift</dt> <dd id="shift">{{ user.Shift == 2 ? 'night' : 'day' }}</dd> </dl> <p id="bio">{{ user.Bio }}</p> <p class="muted">Last opened a page: <span id="last-seen">{{ lastSeen }}</span></p>Every page needs an access rule on its path, in its own template or in a parent's. This page has none of its own:
pages/users/index.htmlholds<ssr:access role="staff"/>, which covers every page below/users. A page outside such a folder starts with its own rule, such as<ssr:access role="staff"/>, or<ssr:access role="*"/>for every viewer. A page with an id in its URL also says whose records it shows, withguard="true"orshared="true"(E-GEN-054): this one inheritsshared="true"from the card above it,pages/users/s_login/index.html, since every member ofstaffsees every profile. -
Declare the types the template names, such as
User, in a.gofile next to it:package info import "people/deps" // User is the user whose details this page shows. type User = deps.User -
Run
aicoded generate; underaicoded dev, saving the file is enough. It writesroute_gen.go, with theRouteDatastruct of the page's values, and adataprovider.gowhoseDatafills nothing yet.dataprovider.gois yours:aicoded generatewrites it only when it is missing. FillData:// Data shows the user's details and when they last opened a page. func (p *DP) Data(ctx context.Context, r *web.Request, _ web.ResponseWriter, data *RouteData) error { u, err := p.d.User(ctx, r.URLParam("login")) if errors.Is(err, deps.ErrNoUser) { return web.NotFound() } if err != nil { return err } data.User = u data.LastSeen = seen(p.d.Seen.At(u.Login)) return nil }// seen tells when a user last opened a page: at, when ok. func seen(at time.Time, ok bool) string { if !ok { return "not since the app started" } return at.UTC().Format("2006-01-02 15:04:05 UTC") }r.URLParam("login")reads the folder's parameter, and returningweb.NotFound()answers 404. -
Link to the page. It shows inside the layouts above it, where they hold
<ssr:content/>. The card,pages/users/s_login/index.html, links to the tab and holds<ssr:content default="info"/>, so/users/aliceopens it.
The page's lastSeen is a live value, which Add a live value explains.
Check it
aicoded checkgenerates, builds, vets, lints and tests the app.aicoded generaterecords the page in theaccesssection ofaicoded.yamlas"/users/{login}/info"withrequire: ["staff"]andshared: true, andaicoded describelists it.- Open
http://people.localhost:8080/users/alice/infoas a persona with thestaffrole. A persona without it gets 403, and a login that no user has gets 404.