k.site.menus
Reading and maintenance of the site's front-end navigation menu (the same set of data as the back-end Development → Menu)
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API / KView / Layout templates | Current site context; get() does not return null when missing, check list() first | Read multi-level navigation trees, resolve localized item names, and manage submenus |
Quick Start
Verify the target menu exists via list(), then call get("main") to read top-level menu items and child links:
k.api.get(() => {
const exists = k.site.menus.list().some((m) => m.name === "main")
if (!exists) return { found: false }
const menu = k.site.menus.get("main")
return {
name: menu.name,
topLevel: (menu.children || []).map((c) => ({
name: c.name,
url: c.url
}))
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
list() | Returns all root menu objects in the site | Menu[] |
get(nameOrId) | Gets root menu by name or ID (returns empty wrapper when missing) | Menu |
create(name) | Creates a new root menu (throws if duplicate name) | Menu |
remove(id) | Deletes a whole root menu by GUID ID | void |
menu.getName(culture) | Returns item name in the target culture (falls back to name) | string |
menu.createSubMenu(name, url) | Adds and saves a child menu item under the current node | void |
menu.updateSubMenu(subMenuId, name, url, culture?) | Updates child name, URL, or culture translation | void |
menu.removeSubMenu(subMenuId) | Deletes a submenu item by subMenuId | void |
Difference from "Edit Menu"
| Item | Location | Purpose |
|---|---|---|
| Develop -> Menus (this API) | /_Admin/development/menus | Site navigation visible to visitors |
| Edit Menu | Top-right user menu | Controls which modules appear in the left sidebar of Kooboo Admin CMS |
See Site Admin Menu Overview for details; item URLs typically point to Pages configured in Develop -> Menus.
Difference from k.site.pages and Missing-Menu Behavior
k.site.menusmanages navigation trees, distinct from page resources and routing in k.site.pages.- When the target menu does not exist,
k.site.menus.get(nameOrId)does not returnnull; it returns a truthyMenuwrapper whose underlying record is empty (!!menu === true). Readingmenu.id,menu.name, ormenu.urlon that wrapper throwsObject reference not set to an instance of an object.. Checkk.site.menus.list()first or wrap access intry/catch.
Detailed Methods
get()
Gets the root menu object (with children tree) by menu name or ID (GUID string).
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Root menu name or ID |
Returns: Menu
k.api.get(() => {
const exists = k.site.menus.list().some((m) => m.name === "main")
if (!exists) return null
const menu = k.site.menus.get("main")
return { id: menu.id, name: menu.name, childCount: (menu.children || []).length }
})list()
Returns an array of all root menu objects of the site (excluding flat lists other than individually expanded subtrees).
None
Returns: Menu[]
k.api.get(() => {
const menus = k.site.menus.list()
return { count: menus.length, names: menus.map((m) => m.name) }
})create()
Creates a new root menu; throws exception if name already exists.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | yes | Root menu name |
Returns: Menu
k.api.post(() => {
const name = "doc-menu-" + Date.now()
const menu = k.site.menus.create(name)
return { id: menu.id, name: menu.name }
})remove()
Delete the entire menu by root menu Id (GUID string).
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | yes | Root menu ID |
Returns: void
k.api.post(() => {
const menu = k.site.menus.create("doc-menu-del")
k.site.menus.remove(menu.id)
return { removed: true }
})Menu Instance
For objects returned by get() / create() / list(), fields can be read and child items maintained at runtime (changes are persisted back to the site database).
| Member | Description |
|---|---|
id | Current node ID |
name | Display name (default language) |
url | Link; root menu item is usually # or empty |
children | Array of submenu items |
source | Fixed to "menu", used for data tracking |
getName(culture) | Display name in the specified language; falls back to name when no translation exists |
createSubMenu(name, url)
Add a new sub-item under the current node and save it.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | yes | Submenu display name |
url | string | yes | Submenu link |
Returns: void
k.api.post(() => {
const root = k.site.menus.get("main")
root.createSubMenu("New Section", "/new-section")
return { childCount: root.children.length }
})updateSubMenu(subMenuId, name, url, culture?)
Updates the name or URL by subMenuId; passing culture updates Values[culture] (and also updates name when culture equals the site's default culture); omitting culture updates name directly.
| Parameter | Type | Required | Description |
|---|---|---|---|
subMenuId | string | yes | Submenu ID |
name | string | yes | New display name |
url | string | yes | New link |
culture | string | no | Culture code |
Returns: void
k.api.post(() => {
const root = k.site.menus.get("main")
const child = root.children[0]
root.updateSubMenu(child.id, "Updated", "/updated")
return { updated: true }
})removeSubMenu(subMenuId)
Delete a submenu item by subMenuId.
| Parameter | Type | Required | Description |
|---|---|---|---|
subMenuId | string | yes | Submenu ID |
Returns: void
k.api.post(() => {
const root = k.site.menus.get("main")
const child = root.children[0]
root.removeSubMenu(child.id)
return { removed: true }
})Frontend Rendering
After the menu is configured in the Admin CMS, choose any approach in templates to render it (you can combine both: <menu> components for the top nav bar and scripts for the footer).
Render with the <menu> Component
Insert the Kooboo Menu component in a Layout or Page: the tag name is menu, and id is the Admin CMS menu name. Rendering uses the HTML templates and placeholders ({href}, {anchortext}, {items}, etc.) configured in Develop -> Menus and automatically handles active-page highlighting and multilingual URLs.
<!-- Render the menu named main (fixed 9-level depth limit in the OpEngine renderer) -->
<menu id="main"></menu>Dragging Menu from the components panel in the layout/page designer has the same effect. When id is invalid or the menu does not exist, nothing is rendered at that location. The <menu> component renders with a fixed depth limit of 9 levels in the OpEngine renderer (attributes such as menulevel are ignored); to implement custom depth limiting, use k.site.menus in a script/template as shown below.
Render with k.site.menus and k-for
Read the menu in a server script and use k-for from Template Binding Syntax so the markup structure is completely under your control.
<script env="server">
var exists = k.site.menus.list().some(function (m) { return m.name === "main"; });
var items = exists ? (k.site.menus.get("main").children || []) : [];
</script>
<nav class="site-nav">
<ul>
<li k-for="item in items">
<a k-attribute="href {item.url}" k-content="item.name"></a>
</li>
</ul>
</nav>For multi-level submenus, nest another k-for over item.children, or extract a recursive View fragment.
Example of multilingual display names:
<script env="server">
var culture = k.site.multilingual.currentCulture;
var exists = k.site.menus.list().some(function (m) { return m.name === "main"; });
var rawItems = exists ? (k.site.menus.get("main").children || []) : [];
var items = rawItems.map(function (c) {
return { name: c.getName(culture), url: c.url };
});
</script>
<ul>
<li k-for="item in items">
<a k-attribute="href {item.url}" k-content="item.name"></a>
</li>
</ul>Method 3: Maintain Menus in API / Script
Calling create, createSubMenu, and related methods in a script or API is suitable for setup wizards and migration scripts; for day-to-day navigation, maintain menus in Admin CMS -> Develop -> Menus.
TypeScript Definitions
interface KMenus {
get(nameOrId: string): Menu;
create(name: string): Menu;
list(): Menu[];
remove(id: string): void;
}
interface Menu {
id: string;
name: string;
url: string;
source: string;
children: Menu[];
getName(culture: string): string;
createSubMenu(name: string, url: string): void;
updateSubMenu(
subMenuId: string,
name: string,
url: string,
culture?: string
): void;
removeSubMenu(subMenuId: string): void;
}Related Docs
- k.site — Overview of site resources
- Menus (CMS) — Create menus, menu items, HTML templates, and permissions in the admin UI
- Template Binding Syntax —
<script env="server">,k-for,k-content - k.site.pages — Page resources that menu items often link to
- Develop Overview — Development grouping and menu entry