k.site.pages
Create, read, update, delete, manage routes, and inspect version logs for site pages
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API / Server scripts | Current site context; repository-level page operations (use k.page for current-page meta) | Programmatically create/update site pages, resolve routes or localized URLs, and inspect change logs |
Quick Start
Create a page with an explicit route and immediately query it via getByUrl() and getUrl():
k.api.post(() => {
const stamp = Date.now().toString()
const name = "doc-check-" + stamp
const url = "/doc-check-" + stamp
k.site.pages.add({
name,
url,
body: `<layout id="main"><placeholder id="Main"><h1>${name}</h1></placeholder></layout>`
})
const page = k.site.pages.getByUrl(url)
return {
created: !!page,
id: page?.id,
url: page ? k.site.pages.getUrl(page.id) : null
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
add(page) | Creates a page and registers its route (defaults to /{name}) | void |
all() | Returns all Page objects in the site | Page[] |
get(nameOrId) | Retrieves a page by name or GUID ID | Page | null |
getByUrl(url) | Retrieves a page by route URL (resolves route aliases; throws NullReferenceException if no route matches) | Page | null |
update(page) | Persists updates on a full Page object | void |
updateBody(nameOrId, body) | Replaces only the page HTML body | void |
delete(nameOrId) | Deletes a page by name or ID | void |
getUrl(id) / getAbsUrl(id) | Returns relative or absolute URL by page ID | string | null |
getUrls(nameOrId) | Returns the per-culture route map for a page | Record<string, string> |
updateRoute(id, url, culture?) | Updates the page route URL for a given culture | void |
getLogs(nameOrId) / getByLog(logId) | Lists version logs and restores a historical snapshot | ChangeLog[] | null / Page | null |
Differences from k.page
| API | Purpose |
|---|---|
k.site.pages | CRUD and URL lookup for all pages in the site (repository scope) |
| k.page | Current rendering page setTitle / setMeta etc. (request scope) |
Detailed Methods
add()
Create a page and register a route. If url is not passed, it defaults to /{name}.
| Parameter | Type | Required | Description |
|---|---|---|---|
page.name | string | yes | Page name (unique) |
page.body | string | yes | Page content, which can be a <layout> placeholder structure |
page.url | string | no | Access path, such as /about; defaults to /{name} when omitted |
page.layoutName | string | no | Associated layout name |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const name = "doc-add-" + stamp
k.site.pages.add({ name, body: `<h1>${name}</h1>` })
const page = k.site.pages.get(name)
return { id: page?.id, url: page ? k.site.pages.getUrl(page.id) : null }
})all()
Returns an array of all page objects on the site.
None
Returns: Page[]
k.api.get(() => {
const pages = k.site.pages.all()
return { count: pages.length, names: pages.slice(0, 5).map((p) => p.name) }
})get()
Get the page by Name or Id (GUID string).
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Page name or ID |
Returns: Page | null
k.api.get(() => {
const page = k.site.pages.get("home")
return page ? { id: page.id, name: page.name } : null
})getByUrl()
Get the page by routing URL (routing aliases will be resolved).
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | yes | Page route URL |
Returns: Page | null
Throws NullReferenceException on unregistered URLs
When no route matches url, k.site.pages.getByUrl(url) does not return null—it throws NullReferenceException (Object reference not set to an instance of an object.). It only returns null when a matching route exists with an empty objectId or broken alias target. Check k.site.routes.getByUrl(url) first or wrap the call in try/catch (which also covers parameterized routes).
// "/" must exist as an exact registered route (does not fall back to /index or /home)
k.api.get(() => {
const targetUrl = "/"
if (!k.site.routes.getByUrl(targetUrl)) {
return null
}
try {
const page = k.site.pages.getByUrl(targetUrl)
return page ? { id: page.id, name: page.name } : null
} catch (e) {
return null
}
})update()
Update with complete page object (usually get / getByUrl first and then change fields).
| Parameter | Type | Required | Description |
|---|---|---|---|
page | Page | yes | Modified full page object |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const url = "/doc-upd-" + stamp
k.site.pages.add({
name: "doc-upd-" + stamp,
url,
body: '<layout id="main"><placeholder id="Main"><h1>v1</h1></placeholder></layout>'
})
const page = k.site.pages.getByUrl(url)
page.body = '<layout id="main"><placeholder id="Main"><h1>v2</h1></placeholder></layout>'
k.site.pages.update(page)
const after = k.site.pages.getByUrl(url)
return {
verified: after?.body?.indexOf("v2") >= 0,
snippet: after?.body?.substring(0, 80)
}
})updateBody()
Only the body field is updated.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Page name or ID |
body | string | yes | New page body HTML |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const url = "/doc-body-" + stamp
k.site.pages.add({
name: "doc-body-" + stamp,
url,
body: "<h1>before</h1>"
})
const page = k.site.pages.getByUrl(url)
k.site.pages.updateBody(page.id, "<h1>after</h1>")
const after = k.site.pages.getByUrl(url)
return {
verified: after?.body?.indexOf("after") >= 0
}
})delete()
Delete pages by name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Page name or ID |
Returns: void
WARNING
Confirm deletion with get(nameOrId). Deleting a page normally removes its associated route, so calling getByUrl on that URL afterwards throws NullReferenceException because the route is null (it only returns null if the route is preserved with an empty objectId because another object still references it).
k.api.post(() => {
const stamp = Date.now().toString()
const url = "/doc-del-" + stamp
const name = "doc-del-" + stamp
k.site.pages.add({ name, url, body: "<h1>del</h1>" })
const before = k.site.pages.getByUrl(url)
k.site.pages.delete(name)
const after = k.site.pages.get(name)
return {
hadPage: !!before,
deleted: !!before && !after
}
})getUrl() / getAbsUrl()
Get a relative path or absolute URL based on the page Id.
| Method | Parameters | Returns | Description |
|---|---|---|---|
getUrl(id) | id: string | string | null | Gets the relative URL |
getAbsUrl(id) | id: string | string | null | Gets the absolute URL |
Returns: string | null
k.api.get(() => {
const page = k.site.pages.all()[0]
if (!page) return { error: "no pages" }
return {
id: page.id,
relative: k.site.pages.getUrl(page.id),
absolute: k.site.pages.getAbsUrl(page.id)
}
})getLogs() / getByLog()
getLogs returns the change log of the specified page; getByLog restores the version object according to the log ID.
| Method | Parameters | Returns | Description |
|---|---|---|---|
getLogs(nameOrId) | nameOrId: string | ChangeLog[] | null | Gets version logs |
getByLog(logId) | logId: number | Page | null | Gets a historical version by log ID |
Returns: ChangeLog[] | null or Page | null
k.api.get(() => {
const page = k.site.pages.all()[0]
if (!page) return { error: "no pages" }
const logs = k.site.pages.getLogs(page.id)
return { logCount: logs ? logs.length : 0 }
})getUrls()
Returns the routing path dictionary of the page in each culture.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Page name or ID |
Returns: Record<string, string>
k.api.get(() => {
const page = k.site.pages.all()[0]
if (!page) return { error: "no pages" }
return k.site.pages.getUrls(page.id)
})updateRoute()
Update the routing path of the page in the specified culture.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | yes | Page ID |
url | string | yes | New path |
culture | string | no | Culture code; defaults to the site's main culture |
Returns: void
// Update the URL for a specific language in a multilingual site
k.api.post(() => {
const page = k.site.pages.get("about")
if (!page) return { error: "page not found" }
k.site.pages.updateRoute(page.id, "/about-us", "en")
return k.site.pages.getUrls(page.id)
})Common Page Fields
| Field | Type | Description |
|---|---|---|
id | string | Page ID |
name | string | Name |
body | string | Page body |
layoutName | string | Layout name |
url | — | Get route via getUrl(id) |
online | boolean | Whether online |
version | number | Version number |
TypeScript Definitions
interface PageRepository {
add(page: PageInput): void;
all(): Page[];
get(nameOrId: string): Page | null;
getByUrl(url: string): Page | null; // Throws NullReferenceException when no route matches url
update(page: Page): void;
updateBody(nameOrId: string, body: string): void;
delete(nameOrId: string): void;
getUrl(id: string): string | null;
getAbsUrl(id: string): string | null;
getLogs(nameOrId: string): ChangeLog[] | null;
getByLog(logId: number): Page | null;
getUrls(nameOrId: string): Record<string, string>;
updateRoute(id: string, url: string, culture?: string): void;
}
interface PageInput {
name: string;
body: string;
url?: string;
layoutName?: string;
}Related Docs
- k.site — Overview of site resources
- k.site.layouts — Layout
- k.page — Current page Meta