Skip to content

k.utils.clientJS ​

Output client-side JavaScript variables and editor/i18n helpers

Execution ContextPrerequisitesTypical Use
Page / Layout / View server rendering (setVariable()); KScript API (monacoResource() / i18n())setVariable() writes directly to HTML RenderOutput and cannot be used in pure JSON API routes; monacoResource() and i18n() return values directly and work in API scripts tooPassing server objects to browser JS, generating Monaco asset tags, and reading site i18n config

k.utils.clientJS provides client-side variable injection along with Monaco resource and i18n helpers. Only setVariable() writes <script> tags into the current HTML response (RenderOutput) to expose server objects via JSON.parse (and is therefore restricted to HTML page/layout/view rendering contexts); monacoResource() and i18n() return plain values directly without accessing RenderOutput, so they can also be used inside k.api.* API routes.

Quick Start ​

js
const pageData = { title: "Catalog", pageSize: 20 }
k.utils.clientJS.setVariable("pageData", pageData)
// Browser scripts can read the global pageData variable directly

Quick Reference ​

Method / PropertyDescriptionReturns
setVariable(key, obj)Inject a JSON-serialized server object into the HTML response as a global variablevoid
monacoResource(callback)Return HTML tags (<script> / <link>) required to load the Monaco editorstring
i18n([all])Return the current site or full multi-language configuration objectany

setVariable() ​

Serialize a server object into JSON and inject it directly into the current HTML response as a global client JavaScript variable (HTML rendering context only).

ParameterTypeRequiredDescription
keystringYesGlobal variable name
objanyYesPlain object, array, or null (do not pass bare strings or booleans)

Returns: void; writes directly to the current HTML response.

Value Type & Context Restriction

  • Pass a plain object, array, or null: The runtime only JSON-serializes non-string reference types; passing a bare string (such as "hello") or boolean (such as true) falls back to .ToString(), producing invalid JSON.parse("hello") or JSON.parse("True") code in the browser. Wrap scalar values in an object (for example { value: "hello", enabled: true }) when needed.
  • HTML rendering context only: Writes <script> tags directly to context.Response.RenderOutput and cannot be used in pure JSON k.api.* routes.
js
const data = { firstname: "Kooboo", count: 3 }
k.utils.clientJS.setVariable("pageData", data)
// Browser output: var pageData = JSON.parse("...")

monacoResource() ​

Returns the resource tag HTML fragment required by the Monaco editor for use by callback onload (returns a string directly without writing to RenderOutput, so it can also be called in API scripts).

ParameterTypeRequiredDescription
callbackstringYesGlobal function name called after the Monaco main script loads

Returns: string, an HTML fragment containing <script> / <link> tags.

js
const resources = k.utils.clientJS.monacoResource("initEditor")
k.response.write(resources)
k.response.write(`
<div id="editor"></div>
<script>
function initEditor() {
  monaco.editor.create(document.getElementById("editor"), {
    value: "console.log('Kooboo')",
    language: "javascript"
  })
}
</script>
`)

i18n() ​

Returns the current site multi-language configuration object (returns a plain dictionary directly without writing to RenderOutput, usable in both pages and API routes).

ParameterTypeRequiredDescription
allbooleanNoWhether to return all language configuration

Returns: any, the current site i18n configuration object.

js
const cfg = k.utils.clientJS.i18n()
const all = k.utils.clientJS.i18n(true)

TypeScript Definitions ​

ts
interface ClientJs {
  setVariable(key: string, obj: any): void;
  monacoResource(callback: string): string;
  i18n(all?: boolean): any;
}