cel-recipes

Sidebars grouped by section, ordering, filters and fallbacks with CEL

Ready-to-use expressions for common jobs. New to expressions? Start with Dynamic fields with CEL.


A sidebar of one section

Give posts a category reference to a Category component, then set the sidebar's post list to:

documents.find("post", {"where": {"category": "docs"}})

where matches a reference by the referenced document's id, URL slug, slug field, code or title, so "docs" finds every post whose category is the Docs category. The sidebar groups the results by category and links each post to /{category}/{post}.

Several sections, in your order

A list in where matches any of its values:

documents.find("post", {"where": {"category": ["docs", "restapi"]}})

That returns both sections, newest post first, so the sections appear in whichever order their newest posts happen to be. To fix the order, run one find per section and join the lists with +:

documents.find("post", {"where": {"category": "docs"}}) +
  documents.find("post", {"where": {"category": "restapi"}})

Docs comes first and Rest API second, always. This is how the sidebar of this site is built: the Docs and Rest API sections share one sidebar, while Tutorials and Change Log have their own.

The current section only

On a /{category}/{post} template, the first part of the URL is the section:

documents.find("post", {"where": {"category": meta.segments[0]}})

One template, and every section's pages get a sidebar of their own section.

Latest posts

find returns newest first, so limit gives the latest:

documents.find("post", {"limit": 3})

Skip the first few with offset, for example to leave out a featured post shown above: {"offset": 1, "limit": 3}.

Fields of the page's document

On a pattern template, doc is the page's own document:

doc.title

References are filled in, so you can reach through them:

doc.category.title + " · " + doc.title

Fallbacks

Use hasProperty and the conditional operator so an empty field never shows as blank:

hasProperty(doc, "description") && doc.description != ""
  ? doc.description
  : "Read " + doc.title

Translated copy

Keep one document and add a translation per language, then read it for the visitor's language:

documents.translated("product", meta.params.slug, meta.locale).name

The second argument identifies the document (id, slug, code or title); the third is the language. Untranslated documents fall back to their original text.


Tips

  • Filter in where, not afterwards. One find with a where is one read; each evaluation may make at most 50.
  • Match references by slug. "docs" is easier to read than an id and keeps working if the category is renamed, as long as its slug stays.
  • Publish the documents too. The live site only sees published documents; the preview shows drafts.
  • Publish the template after changing an expression. The preview shows it right away; the live site picks it up on the next publish.
  • Check the result in the preview. A failing expression keeps the stored value, so a field that does not change is usually an expression that failed.