k.utils.template
View and page rendering
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, Page / View server scripts | Target view or page must exist; executeView() writes to the active response context | Rendering reusable HTML snippets, email bodies, or full pages in scripts |
k.utils.template renders Kooboo View or Page HTML on the server side.
Quick Start
js
k.api.get("renderCard", () => {
const html = k.utils.template.renderView("ProductCard")
k.response.setHeader("Content-Type", "text/html; charset=utf-8")
k.response.write(html || "")
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
render(view) | Render a <view id="..."></view> tag or inline template HTML | string |
executeView(viewNameOrId) | Execute a view by name or ID and write directly to the response context | void |
renderView(viewNameOrId) | Render a view by name or ID and return its HTML string | string | null |
renderPage(url) | Render a full site page by URL (with optional query string) | string |
render()
Renders a <view id="..."></view> tag or inline template HTML string and returns the rendered HTML.
| Parameter | Type | Required | Description |
|---|---|---|---|
view | string | Yes | <view id="..."></view> tag or inline template HTML (bare view names are not resolved as views here; use renderView() for named views) |
Returns: string.
js
const html = k.utils.template.render("<view id=\"...\"></view>")executeView()
Execute a view and write its rendered output directly into the active response context without returning a string.
| Parameter | Type | Required | Description |
|---|---|---|---|
viewNameOrId | string | Yes | View name or ID |
Returns: void.
js
k.utils.template.executeView("ProductCard")renderView()
Render HTML by view name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
viewNameOrId | string | Yes | View name or ID |
Returns: string | null.
js
const html = k.utils.template.renderView("ProductCard")renderPage()
Render a full page of HTML by page URL.
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Site page URL, optionally with query string |
Returns: string.
js
const pageHtml = k.utils.template.renderPage("/products")TypeScript Definitions
ts
interface KTemplateEngine {
render(view: string): string;
executeView(ViewNameOrId: string): void;
renderView(ViewNameOrId: string): string;
renderPage(url: string): string;
}