Utility Filters

Utility Filters

These filters may be used on multiple object types with different results.

default

any defaultValue

If the current value is null, an empty string, or invalid (object.is_valid == false), return the default_value. Otherwise return the original value.

Example: default string

{{ "" | default: "hello" }}
hello

Example: default number

{{ query_params["page"] | default: 1 }}
1

format

string format

Returns the object formatted as a string using the provided format string. There are three types of objects that can be formatted, and each type uses its own format strings:

Numbers

Require a valid standard or custom .NET numeric format string. This is identical to using the format_number filter on a number.

Example: format numbers

{{5.9 | format_number: "F4"}}    5.9000
{{5.9 | format_number: "F0"}}    6
{{5.9 | floor | format_number: "D"}}    5
{{-12445.6789 | format_number: "N"}}    -12,445.68
{{11 | format_number: "D8"}}    00000011
{{1234567890 | format_number: "(###) ###-####"}}    (123) 456-7890
Dates

Require a valid standard or custom .NET date format.

Example: format dates

{{request.date | format: "d"}}    9/9/2009
{{request.date | format: "D"}}    Sunday, September 9, 2009
{{request.date | format: "o"}}    2009-09-09T00:00:00.0000000Z
{{request.date | format: "MMMM dd, yyyy"}}    September 09, 2009
Time Diffs

Require a valid standard or custom.NET TimeSpan format.

Example: format time diffs

{% var diff = request.date | add_days: 3 | time_diff: request.date %}
{{ diff | format: "g" }} => -3:0:00:00
{{ diff | format: "dd 'days'" }} => 03 days

If the input object is a string that can be converted to a date, it will be converted to a date before formatting. Otherwise if the input is a string that can be converted to a number, it will be converted to a number before formatting.

inspect

integer maxDepth = 10 boolean forceLoad = false

Returns a json-like representation of the current object up to maxDepth layers deep (max 10). Will not load new information from the server unless forceLoad is set to true. Useful during template development and debugging, but do not rely on the result of the inspect tag for your live site.

Example: debug an unknown field

{{entity.what_is_this | inspect: 5}}
{{variable | inspect: 3, true}}

object_type

boolean genericObjectCheck = false

Returns a string identifying the type of the input object. If genericObjectCheck is true, will return "object" for most objects. If genericObjectCheck is false or unspecified, will return the value of {{ object.object_type }} for objects with an object_type property. The most common values are:

  • null
  • object
  • string
  • boolean
  • date
  • number
  • list
  • other specific object types (article, blog_post, etc...)

Example: output the type of an unknown variable

{% blog_post post = "post" %}
{{ post | object_type }}    blog_post
{{ post | object_type: false }}    object

rand

integer length = 1 boolean allowRepeats = true boolean preventCache = true

Behaves differently depending on the arguments supplied. Unless preventCache is false, the rand filter will prevent the page from being fast-cached. Note: the rand filter should NOT be considered cryptographically secure - do not use in places where cryptographic security is a requirement (ie: do not use to generate random passwords).

Number

If the input is an integer and length is 1, returns a new integer between 1 and the input value. If the input is an integer and length is greater than 1, returns a new list of length numbers between 1 and the input value. If allowRepeats is false, the list returned will be unique. This may result in a list with fewer than length items if the input is less than length.

Example: Get a random number between 1 and 10

{{ 10 | rand }}

Example: Get a random number between 0 and 10

{{ 11 | rand | minus: 1 }}

Example: Get 4 random numbers between 1 and 10

{{ 10 | rand: 4 }}

Example: Get 4 unique random numbers between 1 and 10

{{ 10 | rand: 4, false }}
String

If the input is a string, returns a new random string with length characters, where each character comes directly from the input. If allowRepeats is false, no characters from the input will be used more than once (although any character repeated in the input may be repeated up to the same number of times in the resulting string) - which may result in a string shorter than length if the input string is shorter than length.

Example: Get a random character from a string

{{ "input" | rand }}

Example: Generate a random alphanumeric string 10 characters long

{{ "abcdefghijklmnopqrstuvwxyz0123456789" | rand: 10 }}
List (includes list-like objects. eg: {{ articles }})

If the input is a list or list-like object and length is 1, returns a random object from the list. If the input is a list or list-like object and length is greater than 1, returns a new random list of length items from the input list. If allowRepeats is false, no items from the input will be used more than once (although any repeated items in the input may be repeated up to the same number of times in the resulting list) - which may result in a list with fewer than length items if the input list has fewer than length items.

Example: Get a random blog post from a list of blog posts

{% blog_posts posts = start: 1 limit: 10 %}
{% var randompost = posts | rand %}

Example: Get 3 random blog posts from a list of blog posts

{% blog_posts posts = start: 1 limit: 10 %}
{% var randomposts = posts | rand: 3, false %}

to_boolean

Converts the input to true or false if possible. If not returns null.

Example: Convert to boolean

{% var show_buttons = query_params['show_btns'] | to_boolean %}