Skip to content

k.page

The <title> and <meta> bindings of the page rendered by the current HTTP request

Overview

k.page writes the header binding in the rendering context of the current request and is output by the layout/page rendering phase to <title> and <meta> of HTML. Suitable for dynamically changing titles or SEO/sharing tags based on interface data and user status.

::: The difference between tip and k.site.pages, k.response.meta

APIeffect
k.pageCurrent page setTitle, setNameMeta, setPropertyMeta, etc. (this page)
k.site.pagesFor the addition, deletion, modification and query of Page resources within the site, see site/page.md
k.response.metaAnother entry bound to the same set of page headers: meta.title, meta.setMeta(name, content), see k.response

k.page.setNameMeta("description", "…") and k.response.meta.setMeta("description", "…") are written into the same binding list; just choose one according to team habits. :::

Must be called in a script that will participate in the HTML rendering of the page (such as the CodeBlock and layout script associated with the page). Pure API returns JSON and does not use page template requests, and the call usually does not affect the response body.

TypeScript Definition

ts
interface KPage {
  setTitle(value: string): void;
  setCharsetMeta(value: string): void;
  setNameMeta(name: string, content: string): void;
  setPropertyMeta(property: string, content: string): void;
  setHttpEquivMeta(equiv: string, content: string): void;
}

setTitle()

Sets or overrides the <title> text of the current page.

ts
k.page.setTitle("Product Details - My Site")
ParameterTypeRequiredDescription
valuestringyestitle text

Returns: void.

setCharsetMeta()

Set document character set meta (write charset binding).

ts
k.page.setCharsetMeta("utf-8")
ParameterTypeRequiredDescription
valuestringyesCharacter set, ignored when empty string

Returns: void.

setNameMeta()

Set <meta name="…" content="…">.

ts
k.page.setNameMeta("description", "Site description")
k.page.setNameMeta("keywords", "kooboo, cms")
ParameterTypeRequiredDescription
namestringyesname attribute
contentstringyescontent attribute

Returns: void.

If the name with the same name already exists, the content will be updated, otherwise the binding will be appended.

setPropertyMeta()

Set <meta property="…" content="…"> (Open Graph, etc.).

ts
k.page.setPropertyMeta("og:title", "Shared title")
k.page.setPropertyMeta("og:image", "https://example.com/cover.jpg")
ParameterTypeRequiredDescription
propertystringyesproperty attribute
contentstringyescontent attribute

Returns: void.

setHttpEquivMeta()

Set <meta http-equiv="…" content="…">.

ts
k.page.setHttpEquivMeta("refresh", "30;url=/next")
k.page.setHttpEquivMeta("X-UA-Compatible", "IE=edge")
ParameterTypeRequiredDescription
equivstringyeshttp-equiv attribute
contentstringyescontent attribute

Returns: void.

Example: Setting up SEO by routing parameters

ts
// Page or layout CodeBlock
const productName = k.request.queryString.name || "Product"
k.page.setTitle(productName + " - Store")
k.page.setNameMeta("description", "Buy " + productName)
k.page.setPropertyMeta("og:title", productName)