Skip to content

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 ContextPrerequisitesTypical Use
KScript API / KView / Layout templatesCurrent site context; get() does not return null when missing, check list() firstRead 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:

js
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 / PropertyDescriptionReturns
list()Returns all root menu objects in the siteMenu[]
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 IDvoid
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 nodevoid
menu.updateSubMenu(subMenuId, name, url, culture?)Updates child name, URL, or culture translationvoid
menu.removeSubMenu(subMenuId)Deletes a submenu item by subMenuIdvoid

Difference from "Edit Menu"

ItemLocationPurpose
Develop -> Menus (this API)/_Admin/development/menusSite navigation visible to visitors
Edit MenuTop-right user menuControls 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.menus manages 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 return null; it returns a truthy Menu wrapper whose underlying record is empty (!!menu === true). Reading menu.id, menu.name, or menu.url on that wrapper throws Object reference not set to an instance of an object.. Check k.site.menus.list() first or wrap access in try/catch.

Detailed Methods ​

get() ​

Gets the root menu object (with children tree) by menu name or ID (GUID string).

ParameterTypeRequiredDescription
nameOrIdstringyesRoot menu name or ID

Returns: Menu

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

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

ParameterTypeRequiredDescription
namestringyesRoot menu name

Returns: Menu

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

ParameterTypeRequiredDescription
idstringyesRoot menu ID

Returns: void

js
k.api.post(() => {
    const menu = k.site.menus.create("doc-menu-del")
    k.site.menus.remove(menu.id)
    return { removed: true }
})

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

MemberDescription
idCurrent node ID
nameDisplay name (default language)
urlLink; root menu item is usually # or empty
childrenArray of submenu items
sourceFixed 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.

ParameterTypeRequiredDescription
namestringyesSubmenu display name
urlstringyesSubmenu link

Returns: void

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

ParameterTypeRequiredDescription
subMenuIdstringyesSubmenu ID
namestringyesNew display name
urlstringyesNew link
culturestringnoCulture code

Returns: void

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

ParameterTypeRequiredDescription
subMenuIdstringyesSubmenu ID

Returns: void

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

html
<!-- 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.

html
<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:

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

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