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.
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.
For some filters, you can provide extra parameters for more flexibility. Add extra parameters by including a = character after the filter name.
truncate removes and extra characters beyond a specified number. For example, {title|truncate=10} will resolve to the first 10 characters of a message.
default allows you to provide a fallback value, in case the data is empty
if resolves to the data, if the parameter is "truthy"
unless resolves to the data, if the parameter is "falsy"
In 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:
copied
{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"}
// Published
We 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.
Data placeholders can reference nested keys in your front matter or other data values.
Here is an example file:
copied
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.
copied
{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 text
We 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.