Configure your card previews

Last modified: April 5th, 2024

Working with a specific static site generator?
Customize CloudCannon's documentation to suit your SSG.

Great! We'll show you documentation relevant to .
You can change this any time using the dropdown in the navigation bar.

What are cards?#

CloudCannon uses cards to represent your data. A card is a configurable UI element that gives you a preview of that data. Depending on the type of data stored, this could include an image, title, subtitle, icon, and metadata.

The anatomy of a card preview

Cards appear in different places across the app, including:

  1. Snippets
  2. Snippet Picker
  3. Structures
  4. Structure Picker
  5. Select-style inputs
  6. Object inputs
  7. Collection file lists

These cards are configurable using the preview key.

Select-style inputs, Object inputs and Structure and non-structure inputs inside Array inputs support a subset of these options. The specifics are documented on each page.

Configure your card previews#

The preview configuration has a number of different keys which correspond to different parts of the card display.

Preview reference#

text - cascading option#

The highest level of text shown in the preview.

subtext - cascading option#

The second highest level of text which is displayed over at most two lines.

icon - cascading option#

Adds an icon from Google’s Material Icons beside the text and subtext on the card. Must match Material Icon name.

image - cascading option#

Defines an image to display beside the text and subtext on the card. Once the image loads it will cover the icon.

metadata - Array#

A list of items that can contain an image, icon and text. Great for related data. See metadata reference below.

Defines large image/icon preview for the card. See gallery metadata below.

metadata reference#

Metadata is a great way to show related data in the card.

The layout of a cards metadata
text - cascading option#

The text to be displayed on the metadata item.

icon - cascading option#

Defines an icon from Google’s Material Icons beside the text on the metadata. Must match Material Icon name.

image - cascading option#

Defines an image path to load over the icon.

The gallery will render an icon if it is defined, an image over the icon once loaded and if neither exist the text will be displayed.

Different states of the gallery option
text - cascading option#

Defines text to display in the centre of the gallery area if there is no icon or image.

icon - cascading option#

Defines an icon from Google’s Material Icons to display in the centre of the gallery area. match Material Icon name.

image - cascading option#

Defines an image to display in the gallery area. Covers text and icon once the image is loaded.

fit - String#

Sets how image is displayed in the gallery. Must be cover (default) or contain.

How cascading options work#

Most of the keys within preview are the same type of structure, which we call cascading options. This is either an Array or a single value of the following types:

  • A String of the exact value (e.g "My Item")
  • A reference to a key within the connected item (e.g key: title in YAML or { "key": "title" } in JSON)
  • A String with placeholders that will be replaced with values (e.g. template: {date|date_long} in YAML or { "template": "{date|date_long}" } in JSON)
copied
# String option
preview_option: Fallback String

# or, Array cascading option
preview_option:
- key: some_key
- key: object.subkey
- template: '{date|date_long}'
- Fallback String

When using an Array each item is read in order. If no content is found, the next item in the array is used. If no content is valid, the corresponding preview option will be hidden.

Below is an example of some data representing a Figure that needs previewed:

copied
_type: figure
image_path: /image.png
alt_text: An image

When displaying this Figure we want to:

  1. Always display the image icon (A String example)
  2. In the text, display the image_path key falling back to the String ‘Image missing’ (An Array example)
  3. In the subtext, display the alt_text key falling back to the String ‘No alternative text’ (Another Array example)

Writing this as a set of preview options will look like this:

copied
preview:
  icon: image
  text:
    - key: image_path
    - Image missing
  subtext:
    - key: alt_text
    - No alternative text

Template strings in previews#

You can use template strings in your card previews. Template strings are a mixture of literal text and dynamic placeholders that are replaced with data. You can use template strings to populate your card previews with more complex strings.

The template key can reference keys from within the object you want to preview.

When using a template string, all placeholders within the string must resolve to an output. If any placeholder resolves to an empty result, CloudCannon cannot display that template string. In this case, CloudCannon will use the next item in the array.

Let's walk through an example. We want to add the author of a file to the card metadata field. We want the metadata field to have a person icon and the text "By [author name]". If no author name is available, the text should read "No author".

To achieve this, we will create an array of metadata.text options. The first array item is our template string, with the value of template set to By {author.name}. The second array item is the string No author.

cloudcannon.config.yaml
copied
collections_config:
  posts:
    preview:
      metadata:
        - text:
            - template: By {author.name}
            - No author
          icon: person
cloudcannon.config.json
copied
{
  "collections_config": {
    "posts": {
      "preview": {
        "metadata": [
          {
            "text": [
              {
                "template": "By {author.name}"
              },
              "No author"
            ],
            "icon": "person"
          }
        ]
      }
    }
  }
}
cloudcannon.config.cjs
copied
module.exports = {
  collections_config: {
    posts: {
      preview: {
        metadata: [
          {
            text: [
              {
                template: "By {author.name}"
              },
              "No author"
            ],
            icon: "person"
          }
        ]
      }
    }
  }
};

In the above example, if the author.name key in our object is set to "Heather", the card preview will display "By Heather" in the metadata field.

For more information, including a list of all available placeholder values, read our documentation on template strings.

Picker specific options#

There are two states where cards can be displayed:

  1. A card connected to an existing item with it’s own data (e.g an instance of a snippet or structure on a page)
  2. A card used in a “picker” where the type of item needs to be selected (e.g. adding a snippet or structure)

When a card is used in a “picker”, it is useful to override the options from the preview config. For this we use the picker_preview config which merges on top of the preview options. The options within picker_preview are the same as the options in preview.

For the same figure example we used above we want to:

  1. Show the String ‘Figure’ for the text
  2. Disable the subtext to prevent showing the String ‘No alternative text’
copied
figure:
  picker_preview:
    text: Figure # Used in a picker
    subtext: false # Disables the subtext in the picker
  preview: # Used in each figure item
    icon: image
    text:
      - key: image_path
      - Image missing
    subtext:
      - key: alt_text
      - No alternative text
Open in a new tab