Skip to content

k-data

Declare data flow with HTML tags inside a template. This is the recommended approach and replaces deprecated k-query.

Overview

k-data uses plain HTML tags such as <k-data>, <let>, and <query> to describe a full server-side data flow: read request values, query data, shape the result, and expose variables. Add the export attribute to the <query> or <let> that creates a value needed by the template.

Compared with the old single-attribute k-query syntax, k-data:

  • has a fixed structure that is easier for AI generation and code review,
  • supports branches, exceptions, redirects, and field mapping,
  • and uses JSON5 conditions for where and test instead of SQL-like strings.

Division of responsibility with template bindings

LayerSyntaxResponsibility
Inside <k-data><let>, <query>, <when> and friendsPrepare data
Template DOMk-content, k-for, k-if, k-attributeRender UI

See Template Binding Syntax for the rendering layer.

Minimal Runnable Example

This example does one thing: query article content and display each title.

html
<k-data>
    <query
        as="articles"
        source="content"
        resource="article"
        action="list"
        export
    ></query>
</k-data>

<ul>
    <li k-for="article in articles">
        <span k-content="article.title"></span>
    </li>
</ul>

This is the smallest complete data flow:

  1. <query> fetches data and stores the result in articles.
  2. The export attribute on <query> exposes articles to the template.
  3. k-for iterates over the result and k-content displays each title.

Before you run it

The site must have a Content Folder named article with a title field. Replace these names with the resource and field names used by your site.

TopicDescriptionDocumentation
Fields and filtersControl query results with select and whereQuery Sources
Request inputRead values from URLs, cookies, headers, and other request sourcesTag Syntax · let
Pagination and sortingUse paged, page-number, and order-descQuery Sources · paged
Mapping and formattingReshape fields and format display valuesTag Syntax · map, Built-in Functions
Conditions and errorsBranches, error handling, and redirectsCondition Expressions, Tag Syntax

Authoring Conventions

  1. Read external input with source plus from, such as queryString or cookie.
  2. Use source, resource, and action for queries. Standardize pagination with page-number and page-size.
  3. For action="paged", map the list with from="result.list".
  4. In the template layer, use only variables exposed with the export attribute.
  5. <when>, <else-if>, and <else> belong only inside <k-data>. In the DOM layer, use k-if, k-else-if, and k-else.
  6. The {...} syntax in k-attribute only interpolates variables. It does not perform calculations. Use <let use="add|subtract|..."> first, then bind the result.
  7. <query> is not an HTML void element. Always include </query>; do not write <query ... />.

Tag Summary

TagResponsibility
k-dataRoot container for the data flow
letVariables, request input, or built-in function calls
queryQuery content, commerce, database, and related sources
mapMap over an array or object
when / else-if / elseConditional branches inside k-data
try / catchException handling
redirectServer-side redirect

export is a boolean attribute on <query> and <let> that exposes their as variable to the template layer.

See Tag Syntax for details.

Docs in This Section

TopicDescription
Tag Syntaxlet, query, map, branching, and exposing variables
Query Sourcescontent, commerce, sqlite, mysql, culture, and more
Built-in FunctionsdateFormat, jwtDecode, math, and text helpers
Condition ExpressionsJSON5 where and test syntax

Relationship to KScript APIs

ScenarioDoc
Declarative data fetching inside a templateThis k-data section
CRUD or business logic in script codek.content, k.DB, k.commerce and related APIs