k.site.layouts
Create, read, update, delete, and inspect version logs for Layout resources
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API / Server scripts | Current site context; Layouts have no standalone HTTP routes | Manage HTML layout skeletons containing k-placeholder regions and inspect version logs |
Quick Start
Create a site layout with a k-placeholder="Main" region and verify it via get():
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-layout-" + stamp
k.site.layouts.add({
name,
body: `<!DOCTYPE html><html><body><div k-placeholder="Main">Layout ${name}</div></body></html>`
})
const layout = k.site.layouts.get(name)
return { verified: !!layout && layout.name === name, id: layout?.id }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
add(layout) | Creates a Layout resource | void |
all() | Returns all Layout resources in the site | Layout[] |
get(nameOrId) | Retrieves a Layout by name or ID | Layout | null |
update(layout) | Updates the full modified Layout object | void |
updateBody(nameOrId, body) | Replaces only the Layout HTML body string | void |
delete(nameOrId) | Deletes a Layout by name or ID | void |
getLogs(nameOrId) | Retrieves version change logs for the Layout | ChangeLog[] | null |
getByLog(logId) | Restores a historical Layout snapshot by log ID | Layout | null |
Difference from routable resources (such as pages)
k.site.layouts provides add, all, get, update, updateBody, delete, and version-log APIs, but it does not provide getByUrl, getUrl, or getAbsUrl. Those URL lookup and link-generation APIs belong to routable repositories such as k.site.pages, k.site.scripts, k.site.styles, and k.site.codes (k.site.images and k.site.files also provide getByUrl).
Detailed Methods
add()
Creates a Layout.
| Parameter | Type | Required | Description |
|---|---|---|---|
layout.name | string | Yes | Layout name; use the layout id when referencing it from a page |
layout.body | string | Yes | HTML that includes k-placeholder or placeholder div elements |
Returns: void
k.api.post(() => {
const name = "landing-layout-" + Date.now()
k.site.layouts.add({
name,
body: '<!DOCTYPE html><html><body><div k-placeholder="Main"></div></body></html>'
})
return { created: !!k.site.layouts.get(name) }
})all()
Returns all Layout resources.
None
Returns: Layout[]
k.api.get(() => {
const layouts = k.site.layouts.all()
return { count: layouts.length, names: layouts.slice(0, 5).map((l) => l.name) }
})get()
Gets a Layout by name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Layout name or ID |
Returns: Layout | null
k.api.get(() => {
const layout = k.site.layouts.get("main")
return layout ? { id: layout.id, name: layout.name } : null
})update()
Updates the full Layout object (typically get first, modify fields, then update).
| Parameter | Type | Required | Description |
|---|---|---|---|
layout | Layout | yes | Modified full Layout object |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-layout-upd-" + stamp
k.site.layouts.add({
name,
body: '<div k-placeholder="Main">v1</div>'
})
const layout = k.site.layouts.get(name)
layout.body = '<div k-placeholder="Main">v2</div>'
k.site.layouts.update(layout)
const after = k.site.layouts.get(name)
return {
verified: after?.body?.indexOf("v2") >= 0,
hasV1: after?.body?.indexOf("v1") >= 0
}
})updateBody()
Updates only the body field.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Layout name or ID |
body | string | yes | New Layout HTML |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-layout-body-" + stamp
k.site.layouts.add({ name, body: "<div>before</div>" })
k.site.layouts.updateBody(name, "<div>after-body</div>")
const after = k.site.layouts.get(name)
return { verified: after?.body?.indexOf("after-body") >= 0 }
})delete()
Deletes a Layout by name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Layout name or ID |
Returns: void
WARNING
After deleting, confirm with get(nameOrId). Do not depend on a nonexistent getByUrl.
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-layout-del-" + stamp
k.site.layouts.add({ name, body: "<div>del</div>" })
const before = k.site.layouts.get(name)
k.site.layouts.delete(name)
const after = k.site.layouts.get(name)
return {
verified: !!before && !after,
hadBefore: !!before
}
})getLogs() / getByLog()
Inspect Layout change logs or restore a snapshot by log entry ID.
| Method | Parameters | Returns | Description |
|---|---|---|---|
getLogs(nameOrId) | nameOrId: string | ChangeLog[] | null | Gets version logs |
getByLog(logId) | logId: number | Layout | null | Gets a historical version by log ID |
Returns: ChangeLog[] | null or Layout | null
k.api.get(() => {
const layouts = k.site.layouts.all()
if (!layouts.length) return { error: "no layouts" }
const logs = k.site.layouts.getLogs(layouts[0].id)
return {
verified: logs === null || Array.isArray(logs),
logCount: logs ? logs.length : 0
}
})getByLog(logId) restores a Layout snapshot from a version log ID, just like k.site.pages.
Layout Fields and Page Usage
Common Layout Fields
| Field | Type | Description |
|---|---|---|
id | string | Layout ID |
name | string | Name |
body | string | HTML content |
extension | string | Usually html |
online | boolean | Whether it is online |
version | number | Version number |
Use with Pages
Example of referencing a Layout from a page body (the main Layout must already exist in k.site.layouts, and its body must define k-placeholder="Main"):
<layout id="main">
<placeholder id="Main">
<h1>Page content</h1>
</placeholder>
</layout>TypeScript Definitions
interface LayoutRepository {
add(layout: LayoutInput): void;
all(): Layout[];
get(nameOrId: string): Layout | null;
update(layout: Layout): void;
updateBody(nameOrId: string, body: string): void;
delete(nameOrId: string): void;
getLogs(nameOrId: string): ChangeLog[] | null;
getByLog(logId: number): Layout | null;
}
interface LayoutInput {
name: string;
body: string;
}