Skip to documentation content

Picks, a Fetch, or Both

Picks, a Fetch, or Both

How a listing is populated: editor-picked items, a fetch with a list method, or picks first with a fetch to fill the rest.

How a listing is populated is a separate choice from who owns its route.
• A picker field fits an editor-curated listing and its order. entitylist selects any content record; datastoreitemlist selects from one datastore. Set HasUrl on an entitylist field when every pick must have its own page: the CMS then offers only entities with a URL and rejects a saved value without one. When the list may mix entities with and without pages, leave the filter off and guard each link with item.has_url.
• A fetch ({% datastore_items %} and the other list methods) is not only "show everything". It can return the most recent items, a filtered or queried subset, or random items, ordered with sort_by (a custom field's FieldID works on datastore_items) or with | sort:. List methods return 10 items unless limit says otherwise (Obtain a Collection).
• They combine well: show the editor's picks first and fill up to a maximum with recent items, or fetch random items when nothing was picked.

Field on the page template (Type datastoreitemlist, DatastoreGUID Services, FieldID featured_services), then:

{%- var max_cards = 6 -%}
{%- var cards = entity.featured_services -%}
{%- if cards.is_valid -%}
{%- datastore_items cards = datastore:'Services' prepend:cards unique:true limit:max_cards max_size:max_cards -%}
{%- else -%}
{%- datastore_items cards = datastore:'Services' sort_by:'random' cache_random:true limit:max_cards -%}
{%- endif -%}
{%- for card in cards -%}
{%- if card.has_url -%}
<a href="{{ card.full_url }}">{{ card.title }}</a>
{%- else -%}
{{ card.title }}
{%- endif -%}
{%- endfor -%}

prepend puts the picks first; unique:true keeps them out of the fetched results; max_size caps the total, lowering the fetch to what is left after the picks. Without an argument such as limit, a call made of prepend alone fetches nothing.