C&C Docs
Log in
Pages

Data blocks — a block that answers JSON

A block can answer JSON instead of a page. Give it one line, <meta name="response" content="json">, and a GET runs its controller with the query string on $req.query and sends the state back as the body: no layout and no page around it. Open /cities raw.

<!-- blocks/cities.html: the whole template -->
<meta name="response" content="json" />
// blocks/cities.mjs
export default {
  search: '',
  matches: [],

  $update() {
    this.search = String(this.$req?.query?.search ?? '')
    const needle = this.search.toLowerCase()
    this.matches = needle ? CITIES.filter((name) => name.toLowerCase().includes(needle)) : []
  }
}

Ask it from the browser