Skip to content

k.utils.template ​

View and page rendering

Execution ContextPrerequisitesTypical Use
KScript API, Page / View server scriptsTarget view or page must exist; executeView() writes to the active response contextRendering 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 / PropertyDescriptionReturns
render(view)Render a <view id="..."></view> tag or inline template HTMLstring
executeView(viewNameOrId)Execute a view by name or ID and write directly to the response contextvoid
renderView(viewNameOrId)Render a view by name or ID and return its HTML stringstring | 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.

ParameterTypeRequiredDescription
viewstringYes<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.

ParameterTypeRequiredDescription
viewNameOrIdstringYesView name or ID

Returns: void.

js
k.utils.template.executeView("ProductCard")

renderView() ​

Render HTML by view name or ID.

ParameterTypeRequiredDescription
viewNameOrIdstringYesView name or ID

Returns: string | null.

js
const html = k.utils.template.renderView("ProductCard")

renderPage() ​

Render a full page of HTML by page URL.

ParameterTypeRequiredDescription
urlstringYesSite 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;
}