cel-fields
Compute block fields from the page's document, its URL and your other documents
Most block fields hold fixed copy. Some should be computed instead: the headline of a product page is the product's name, a sidebar lists every post in a section, a title follows the visitor's language. In the Template Builder, click fx next to a field and write a short CEL expression; its result replaces the stored value whenever the page is read.
What an expression can read
| Name | What it is |
|---|---|
doc | The page's own document on a pattern template (for /blog/{slug}, the post that page is for). |
meta.params | URL parameters by name: meta.params.slug on /blog/{slug}. |
meta.segments | The path split into parts: /docs/intro is ["docs", "intro"]. |
meta.title, meta.docId | The page document's title and id. |
meta.locale | The visitor's language, for translated content. |
documents | Every other document of the website (see below). |
Reading other documents
// One document, by id, slug, code or title
documents.get("author", "jane-doe").name
// The same, written as a reference
documents.ref("product").get(meta.params.slug).price
// The translated copy for the visitor's language
documents.translated("product", meta.params.slug, meta.locale).name
// Every document of a component, newest first
documents.find("post")
// Filtered and paged
documents.find("post", {"where": {"category": "docs"}, "limit": 10})
find returns a list, so it fits list fields such as a sidebar or a related-posts block. Reference fields in the results come back filled in with the referenced document, so post.category.title works directly.
A first expression
On a /products/{slug} template, set the hero's headline to:
doc.name
Every product page now shows its own product's name, and changing the product's name changes the page. No per-page editing.
Give it a fallback when the field may be empty:
hasProperty(doc, "tagline") && doc.tagline != "" ? doc.tagline : doc.name
Drafts and publishing
- The preview and the Template Builder always show the expression you saved last.
- The live site keeps the expression its last publication recorded. Saving a new expression marks the template as changed; it goes live when you Publish the template.
documents.*reads published documents on the live site and drafts in the preview, so an unpublished document only shows in the preview.
When an expression fails
A failing expression never breaks a page: the field keeps its stored value and the error is logged. Saving checks the syntax first, so typos are caught in the editor.
Each evaluation is limited to 5 seconds, 50 document reads and 5000 characters of source. Keep expressions small, and prefer one find with a where over many get calls.
Next
See CEL recipes and tips for sidebars grouped by section, ordering, filters and more.