Skip to content

k.site.layouts ​

Create, read, update, delete, and inspect version logs for Layout resources

Execution ContextPrerequisitesTypical Use
KScript API / Server scriptsCurrent site context; Layouts have no standalone HTTP routesManage 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():

js
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 / PropertyDescriptionReturns
add(layout)Creates a Layout resourcevoid
all()Returns all Layout resources in the siteLayout[]
get(nameOrId)Retrieves a Layout by name or IDLayout | null
update(layout)Updates the full modified Layout objectvoid
updateBody(nameOrId, body)Replaces only the Layout HTML body stringvoid
delete(nameOrId)Deletes a Layout by name or IDvoid
getLogs(nameOrId)Retrieves version change logs for the LayoutChangeLog[] | null
getByLog(logId)Restores a historical Layout snapshot by log IDLayout | 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.

ParameterTypeRequiredDescription
layout.namestringYesLayout name; use the layout id when referencing it from a page
layout.bodystringYesHTML that includes k-placeholder or placeholder div elements

Returns: void

js
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[]

js
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.

ParameterTypeRequiredDescription
nameOrIdstringyesLayout name or ID

Returns: Layout | null

js
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).

ParameterTypeRequiredDescription
layoutLayoutyesModified full Layout object

Returns: void

js
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.

ParameterTypeRequiredDescription
nameOrIdstringyesLayout name or ID
bodystringyesNew Layout HTML

Returns: void

js
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.

ParameterTypeRequiredDescription
nameOrIdstringyesLayout name or ID

Returns: void

WARNING

After deleting, confirm with get(nameOrId). Do not depend on a nonexistent getByUrl.

js
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.

MethodParametersReturnsDescription
getLogs(nameOrId)nameOrId: stringChangeLog[] | nullGets version logs
getByLog(logId)logId: numberLayout | nullGets a historical version by log ID

Returns: ChangeLog[] | null or Layout | null

js
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 ​

FieldTypeDescription
idstringLayout ID
namestringName
bodystringHTML content
extensionstringUsually html
onlinebooleanWhether it is online
versionnumberVersion 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"):

html
<layout id="main">
  <placeholder id="Main">
    <h1>Page content</h1>
  </placeholder>
</layout>

TypeScript Definitions ​

ts
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;
}