What are template strings?#
Template strings are a mixture of literal text and dynamic placeholders that are replaced with data. You can use template strings to populate complex strings.
Template strings are commonly used in:
- Configuring your Create paths for new files
- Configuring your Uploads paths for new assets
- Configuring your card previews
- Configuring your commit messages
Please read these articles for more specific information about the best placeholders for each use case.
Placeholders#
Placeholders reference front matter or data values. When CloudCannon reads a placeholder, it will replace the content with the data it references.
There are two types of placeholders.
- Data placeholder — Reads data you defined in your front matter or other data values. These placeholders use
{ }brackets. - Fixed placeholder — Reads fixed data defined by CloudCannon. These placeholders use
[ ]brackets.
Filters#
Placeholders support several filters. Filters occur after the placeholder key, with a | character separating each key and filter. You can use multiple filters in sequence.
Generic filters
uppercasetransforms text to uppercaselowercasetransforms text to lowercasedeburrconverts Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removes combining diacritical marks.slugifyconverts non-alphanumeric characters into hyphens, then collapses sequential hyphens, then removes leading/trailing and hyphens.trimremoves leading and trailing whitespace
Date filters
yeargets a 4-digit year from a datemonthgets a 2-digit month from a datedaygets a 4-digit day from a datetimegets the time from a date, in a 12-hour format (e.g. 12:30pm)timezonegets the timezone from a datedate_shortgets a short date format (e.g. 7/12/2022)date_mediumgets a medium date format (e.g. 7 Dec 2022)date_longgets a long date format (e.g. 7 December 2022)date_fullgets a longer date format (e.g. Tuesday, 7 December 2022)timegets the time (e.g. 1:00pm)time_shortgets a short time format (e.g. 1:00 pm)time_mediumgets a medium time format (e.g. 1:00:00 pm)time_longgets a long time format (e.g. 1:00:00 pm NZDT)time_fullgets a longer time format (e.g. 1:00:00 pm New Zealand Daylight Time)
All the time and date filters format the date using the site's timezone and the user's locale.
Filters with parameters
For some filters, you can provide extra parameters for more flexibility. Add extra parameters by including a = character after the filter name.
truncateremoves any extra characters beyond a specified number. For example,{title|truncate=10}will resolve to the first 10 characters of a message.defaultallows you to provide a fallback value, in case the data is emptyifresolves to the data, if the parameter is "truthy"unlessresolves to the data, if the parameter is "falsy"substringextracts a section of a string, using the JavaScript substring method. You can specify a start and end index separated by a comma. For example,{title|substring=1}resolves to every character after the first.{title|substring=1,2}resolves to the 1 character only.strip_leading_dateremoves a date from the start of a string (or after a single/). The date can be any string composed of three numbers, where one number is 4 digits long and the other two are 1 or 2 digits, and which are separated by dashes, underscores, periods, or commas. For example"/2025-12-01-blog.md"|strip_leading_datewill resolve to/blog.md.
Filtering examples
Let's go through some examples for the following file:
date: 2020-01-02
empty_value:
message: "Hello World"
draft: falseIn the above file we have four keys containing different data types: date, empty_value, message, and draft.
Here are some examples of how we can transform this data using different filters:
{date|day}-{date|month}-{date|year} {date|time} ({date|timezone})
// 01-02-2020 00:00am (Etc/UTC)
{message|uppercase|truncate=5}
// HELLO
{message|if=message}
// Hello World
{message|unless=empty_value}
// Hello World
{empty_value|default="Nothing here!"}
// Nothing here!
{"Unpublished"|if=draft|default="Published"}
// PublishedWe can use multiple date filters to control the output string. This template string uses dynamic data placeholders and literal text characters, such as - and ().
We can convert text strings to uppercase and truncate the number of characters to five.
We can print the value of a key if a value exists using the if filter.
We can print the value of a key based on the value of another key using the unless filter. In this case, we print the value of message because empty_value is falsy.
We can create a fallback option when a placeholder resolves to an empty result using the default filter. In this case, "Nothing here!" is printed because empty_value contains no value.
We can use multiple filters to check if a key contains a value and create a fallback string if that key is falsy.
Filtering nested keys and arrays
Data placeholders can reference nested keys in your front matter or other data values.
Here is an example file:
links:
- url: /documentation
text: Learn more
- url: /blog
text: Read more
- url:
text:
seo:
description: Description goes here
tags:
- 'sales'
- 'documentation'In the above file, we have several nested keys and arrays. You can reference these keys in a template string by using the relative path of the key, by specifying the position within an array, or by searching each item in an array using [*].
Here are some examples of how we can transform this data using different filters:
When you use [*] to reference each item within an array, CloudCannon will join the final output into a single string, separating each value with a comma and a space.
{seo.description}
// Description goes here
{links.text[0].description}
// Learn more
{seo.tags[*]|uppercase}
// SALES, DOCUMENTATION
{links[*].text|default="No text"}
// Learn more, Read more, No textWe can print the value of a nested key.
We can print the value of a specific key within an array.
We can print the value of every item within an array and filter the result with uppercase. CloudCannon automatically joins the final output into a single string.
We can search an array using and print the value of a key each time it appears in that array. If one key is empty, we can use a fallback value using the default filter.