k.utils.clientJS
Output client-side JavaScript variables and editor/i18n helpers
| Execution Context | Prerequisites | Typical 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 too | Passing 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
const pageData = { title: "Catalog", pageSize: 20 }
k.utils.clientJS.setVariable("pageData", pageData)
// Browser scripts can read the global pageData variable directlyQuick Reference
| Method / Property | Description | Returns |
|---|---|---|
setVariable(key, obj) | Inject a JSON-serialized server object into the HTML response as a global variable | void |
monacoResource(callback) | Return HTML tags (<script> / <link>) required to load the Monaco editor | string |
i18n([all]) | Return the current site or full multi-language configuration object | any |
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).
| Parameter | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Global variable name |
obj | any | Yes | Plain 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-stringreference types; passing a barestring(such as"hello") orboolean(such astrue) falls back to.ToString(), producing invalidJSON.parse("hello")orJSON.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 tocontext.Response.RenderOutputand cannot be used in pure JSONk.api.*routes.
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).
| Parameter | Type | Required | Description |
|---|---|---|---|
callback | string | Yes | Global function name called after the Monaco main script loads |
Returns: string, an HTML fragment containing <script> / <link> tags.
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).
| Parameter | Type | Required | Description |
|---|---|---|---|
all | boolean | No | Whether to return all language configuration |
Returns: any, the current site i18n configuration object.
const cfg = k.utils.clientJS.i18n()
const all = k.utils.clientJS.i18n(true)TypeScript Definitions
interface ClientJs {
setVariable(key: string, obj: any): void;
monacoResource(callback: string): string;
i18n(all?: boolean): any;
}Related Docs
- k.label — multilingual labels